Yes, and those "k = m" lines are the problem! Those assume unsigned* types! And 64-bit integers! Those multiplications and other operations are all wrong!
You didn't port this to Java, you ported a broken algorithm to Java. Way to go, though.
You didn't port this to Java, you ported a broken algorithm to Java. Way to go, though.