Results 1 to 4 of 4

Thread: help needed

  1. #1
    amarchandar is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default 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

  2. #2
    neeraj.singh is offline Member
    Join Date
    Feb 2009
    Location
    Delhi
    Posts
    63
    Rep Power
    0

    Default

    Quote Originally Posted by amarchandar View Post
    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.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by amarchandar View Post
    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.

  4. #4
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    I have just recently made a factor-related program. I'll show you a few methods that I have from it:
    Java 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 :
    Java 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
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

Similar Threads

  1. a little help needed..
    By litojs in forum New To Java
    Replies: 1
    Last Post: 11-16-2008, 07:12 PM
  2. help needed!!! :S
    By mark-mlt in forum Networking
    Replies: 1
    Last Post: 04-14-2008, 09:27 AM
  3. Help needed.
    By necro-1000 in forum AWT / Swing
    Replies: 2
    Last Post: 01-12-2008, 11:37 AM
  4. Help Needed
    By pks in forum New To Java
    Replies: 1
    Last Post: 01-09-2008, 12:07 AM
  5. help needed.
    By dirtycash in forum New To Java
    Replies: 3
    Last Post: 12-03-2007, 09:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •