2 Replies - 2439 Views - Last Post: 18 October 2010 - 12:38 PM Rate Topic: -----

#1 Alaeor   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-October 10

Largest Prime Factor Finder

Posted 18 October 2010 - 09:20 AM

I've been working on this in class for awhile, almost a month sadly (long past the due date). When you input a number, it is supposed to tell if it's prime, and if not, what it's largest prime factor is. Right now, some numbers report incorrectly (9 is prime, etc.) and it lists all of the factors of a number. What should I be doing to fix this?
import java.util.InputMismatchException;
import java.util.Scanner;
public class Prime_Finder {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int a_number;
		int primefactor;
		int j;
	while (true) {
		boolean prime = true;
		System.out.println("Enter a positive integer to see it's factors or a negative integer to exit.");
		
		while (true) {
		      try {
		    	  		
					a_number = scan.nextInt();
		    	  }
		    	  catch ( InputMismatchException e ) {
		    	    System.out.println("Error 1: Invalid, must be a whole number such as 12, please try again." );
		    	    scan.nextLine(); // clear the buffer
		    	    continue;
		    	  }
		    	  break;
		      }	
		
		if (a_number <= -1)
			break;
		if (a_number <= 1)
		{	System.out.println("The number 1 is prime.");
			}
		while (true){
			for (int i = 2; i < a_number+1; i++)
				if (a_number%i == 0) { 							
					for (j = 2; j < i; j++){
						if (i%j == 0)
					{ prime = false; }
				}
			if (prime == true) {
				
				System.out.println("The number " +a_number+ " is prime, and has no factors besides " +a_number+ " and 1");
				break;}
			else {
				primefactor = i;
				
				
				System.out.println("A factor of " +a_number+ " is " +i+ ",");
			
						}
			}
		break;
		}
	}
	System.out.println("Goodbye");
}
}


This post has been edited by Alaeor: 18 October 2010 - 09:22 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Largest Prime Factor Finder

#2 NoobKnight   User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: Largest Prime Factor Finder

Posted 18 October 2010 - 11:51 AM

The problem (I think) is your curly braces '{' and '}'
You don't have them in the right positions.
But on a positive note, the meat of your code works.
for (int i = 2; i < a_number+1; i++)
	if (a_number%i == 0) { 							
	   for (j = 2; j < i; j++){
	   if (i%j == 0)
		{ prime = false; }
	   }
}


If you want, just grab the important code above and test it for yourself.
Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Largest Prime Factor Finder

Posted 18 October 2010 - 12:38 PM

You don't need a while(true) statement around your factoring algorithm. The algorithm I'd use is:
while play again
    get input
    determine is prime
    print out largest prime factor
    determine if play again



Also, you can streamline your algorithm some. Just test for primality by seeing if the input is evenly divisible by any number from 2-sqrt(n). If it is, then it isn't prime and you need to test against prime numbers. However, you can use this to find the largest prime factor by generating primes with that loop as well. Take a look at adapting the Sieve of Eratosthenes algorithm for this.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1