4 Replies - 2785 Views - Last Post: 02 December 2012 - 05:42 PM Rate Topic: -----

#1 RidleyMoose   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-December 12

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?

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)/>;
                
        }



Is This A Good Question/Topic? 0
  • +

Replies To: Java Greatest Common Factor Two Numbers

#2 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2770
  • View blog
  • Posts: 4,429
  • Joined: 21-June 11

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.

This post has been edited by sepp2k: 02 December 2012 - 04:57 PM

Was This Post Helpful? 1
  • +
  • -

#3 RidleyMoose   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-December 12

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.
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2770
  • View blog
  • Posts: 4,429
  • Joined: 21-June 11

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.
Was This Post Helpful? 0
  • +
  • -

#5 RidleyMoose   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-December 12

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1