public static int maxFactor(int a, int B)/>
{
System.out.println("Enter two numbers and I will find the largest prime factor.");
Scanner kb = new Scanner(System.in);
a = kb.nextInt();
b = kb.nextInt();
int g = gcd(a,B)/>;
int divider = 2;
int prime = 1;
while(g >= divider)
{
if (g%divider == 0)
{
prime = divider;
g /= divider;
}
else
{
divider++;
}
}
System.out.println(g);
return g;
}
public static int gcd(int a, int B)/>
{
if (b == 0)
{
return a;
}
return gcd(b, a%B)/>;
}
Java Greatest Common Factor Two Numbers
Page 1 of 14 Replies - 2785 Views - Last Post: 02 December 2012 - 05:42 PM
#1
Java Greatest Common Factor Two Numbers
Posted 02 December 2012 - 03:25 PM
Hello, trying to figure out why this program isn't working. Most likely a logic error because I am terrible at gcds and primes. Please take a look?
Replies To: Java Greatest Common Factor Two Numbers
#2
Re: Java Greatest Common Factor Two Numbers
Posted 02 December 2012 - 04:46 PM
Why do you define a method with two parameters if you simply ignore those parameters and replace them with input from the keyboard?
Also it would help if you would explain in what way your method isn't working. How do you use it, what do you expect to happen, what happens instead? If what happens instead is an error, please post the exact error message. If it's wrong output, post your input and the resulting output (plus the expected output and why you expected that output).
PS: You never do anything with the prime variable. If prime is intended to be your result (which would make sense), you should be returning it instead of g at the end.
PPS: There's no need to both print and return the result at the end. The printing should happen at the call site.
Also it would help if you would explain in what way your method isn't working. How do you use it, what do you expect to happen, what happens instead? If what happens instead is an error, please post the exact error message. If it's wrong output, post your input and the resulting output (plus the expected output and why you expected that output).
PS: You never do anything with the prime variable. If prime is intended to be your result (which would make sense), you should be returning it instead of g at the end.
PPS: There's no need to both print and return the result at the end. The printing should happen at the call site.
This post has been edited by sepp2k: 02 December 2012 - 04:57 PM
#3
Re: Java Greatest Common Factor Two Numbers
Posted 02 December 2012 - 05:12 PM
The program reads two integers and is supposed to return the greatest prime factor of the two numbers.
The output of the program always returns 1 as the prime gcd, which is incorrect.
The output of the program always returns 1 as the prime gcd, which is incorrect.
#4
Re: Java Greatest Common Factor Two Numbers
Posted 02 December 2012 - 05:18 PM
You repeatedly divide g by prime numbers until you can't anymore. At the end of that, g will always be 1. As I indicated in my PS earlier, I don't think you meant for g to be the result of the method.
#5
Re: Java Greatest Common Factor Two Numbers
Posted 02 December 2012 - 05:42 PM
I see! Thank you. New to Java and to this site so sorry for any formatting spoofs or errors in etiquette.
Page 1 of 1

New Topic/Question
Reply


MultiQuote


|