# help needed

• 02-17-2009, 10:42 AM
amarchandar
help needed
Hi friends i'm new to java
and i need help to write program that will find prime factors of a given number by using methods
• 02-17-2009, 12:03 PM
neeraj.singh
Quote:

Originally Posted by amarchandar
Hi friends i'm new to java
and i need help to write program that will find prime factors of a given number by using methods

hi..
your question is not dependent on the java... first develop a logic... for that use ur paper and pen.. then try to implement into java.

then, if you find any prob then raise the query.
• 02-17-2009, 02:04 PM
Eranga
Quote:

Originally Posted by amarchandar
Hi friends i'm new to java
and i need help to write program that will find prime factors of a given number by using methods

What you have done so far? Try to workout something first lol. Then ask your question more clearly, where you stuck is.
• 02-17-2009, 02:59 PM
MK12
I have just recently made a factor-related program. I'll show you a few methods that I have from it:
Code:

```private int[] getFactors(int number) {         ArrayList<Integer> factorList = new ArrayList<Integer>();         int numOfFactors = 0;         Integer[] factors;         for(int i = 1; i <= number; i++) {             if(i % number == 0) {                 factorList.add(new Integer(i));                 numOfFactors++;             }         }         factorList.trimToSize();         if(numOfFactors == 2) isPrime = true;         else isPrime = false;         factors = new Integer[factorList.size()];         return integerArrayToIntArray(factorList.toArray(factors)); }```
This gets a numbers factors(doesn't just get the prime ones) but I have another method that gets all the prime numbers from start to end arguments :
Code:

```public int[] getPrimeNumbers(int start, int end) {         ArrayList<Integer> primeNumberList = new ArrayList<Integer>();         Integer[] primeNumbers;         boolean b = isPrime;         for(int i = start; i <= end; i ++) {             getFactors(i);             if(isPrime) {                 primeNumberList.add(new Integer(i));             }         }         isPrime = b;         primeNumberList.trimToSize();         primeNumbers = new Integer[primeNumberList.size()];         return integerArrayToIntArray(primeNumberList.toArray(primeNumbers)); }```
Maybe those will help you get started, look over and see how I did it. Then write your own that only gets prime factors (do NOT just copy and paste mine). Hope this helps.
-MK12