Jump to content

prime factor help

- - - - -

  • Please log in to reply
5 replies to this topic

#1
Cruel Hand

Cruel Hand

    Learning Programmer

  • Members
  • PipPipPipPip
  • 109 posts
  • Programming Language:Java
  • Learning:Java, Visual Basic .NET
I need to write a program that finds the prime factors of a certain number. I have the code for finding the factors of a number, but I don't know how to single out the numbers and determine if they're prime. This is the code I have thus far:

public class PrimeFactor {

	public static void main(String[] args){

		double i = 400, factor = 0;

		for(double counter = 2; counter <= i; counter++){

			if(i % counter == 0){

				factor = i / counter;

				System.out.println(factor);

				}

			}

		}

	}

can anyone point me in the right direction for my next step?

Thanks :D

Edited by Cruel Hand, 26 November 2011 - 02:17 PM.
mis-spelled word


#2
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
If you working with large numbers this may not be very efficient. Best to look up sieve of eratosthenes. This is what i used:

public int isPrime(int x)
{ 
    if( x == 1 ) return 1; /* if you are considering one as prime */ 
    if( x == 2 ) return 1; 
    if( x %  2 == 0) return 0; 
    for(int i = 3; i * i <= x; i = i + 2) /* increment by two; only capture odd numbers
      if( x % i == 0) return 0;   
    return 1; 
}

Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
If you change the if to a while, you can divide i by counter so that you divide out all the prime factors as you find them, thus eliminating the chance of composites.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
Cruel Hand

Cruel Hand

    Learning Programmer

  • Members
  • PipPipPipPip
  • 109 posts
  • Programming Language:Java
  • Learning:Java, Visual Basic .NET
@Fread, idk what the # means in java

@wingedpanther, do you mean like this?

while(i % counter == 0){

				factor = i / counter;

				System.out.println(factor);

				}

or did I misunderstand you?

as you can both tell, my skills in java are novice at best haha

#5
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts

Quote

@Fread, idk what the # means in java
That is just x mod 2. The percent sign prints out like that sometimes if there is no space after it.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
That's exactly what I meant.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users