• Post Reply Bookmark Topic Watch Topic
  • New Topic

Common prime divisors of two number  RSS feed

 
Milan Milanovic
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to find the product of all common divisors of two numbers that are at the same time prime numbers.
For example, 16 and 26 will return 2 as a product of all common PRIME divisors.

Code below for the input of 16 and 24 returns 32, which is obviously wrong.



 
Milan Milanovic
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more thing, I expect it to be done following this logic.
I will appreciate a point out to the mistake or eventual invalid logic in my code.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My suggestion would be to put in a bunch of println statements to see what your code is actually doing.

for starters, are you sure your isPrime is correct? have you tested it thoroughly?

next, i don't think you need the two if statements.  After all, if n1 is not less than n2, what other scenario could there be?  an if-else would make more sense. 

third, since your lines 27-32 and 37-41 are essentially identical, you should move the code into a method.  Then you can pass in two parameters as either (n1,n2) or (n2,n1).  By not having duplicate code, you only need to debug it once. The number of times i have had duplicate code and fixed one set but not the other is too embarrassing to write down.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!