Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Need Help - Factoring & Prime Finding Code

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post

    Thumbs down Need Help - Factoring & Prime Finding Code

    Hello, I'm working on a project that outputs the numbers 2 – 100.
    Next to each number, it should list all the factors for that number.
    If the number has no factors, it should print prime.
    Ex.
    2 prime
    3 prime
    4 2
    5 prime

    Having some difficulty so far. I'm aware I need to use loops, but I'm not sure what to %, and by what to determine if it's a factor.

    This is my fail code so far: (I realize its horrible and is incomplete)
    public class Prime
    {
    public static void main(String[] args)
     {
     	findPrime(factors());
     	factors();
     }
     public static void findPrime(int k)
      {
      	int i = 2;
      	int ans = i%k;	
     	while(i<=100)
     	{
      		System.out.println(ans);
     		i++;
      	}
     
      }
    public static int factors()
      {
      	int k = 1;
      	for(k = 1; k <=100; k++)
      	{   		 	
      	}
      	return k;
      }
    }

    Any help whatsoever is greatly appreciated. Thanks!


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need Help - Factoring & Prime Finding Code

    mmm... prime numbers

    The easiest way to do this is probably seeing if for some number 1<n<101, printing out any numbers that n%i == 0 (where i is a number from 2 to n-1). If nothing was printed out, then print out prime. You can keep track of nothing being printed out by having a boolean flag to denote the primality of the number.

    for (int n = 2; n <= 100; n++)
    {
        boolean isPrime = true; // assume n is prime
        System.out.print(n + " : ");
        for(int i = 2; i < n; i++)
         {
               if (n%i == 0)
               {
                          System.out.print(i);
                          isPrime = false;
               }
         }
         if (isPrime)
         {
              System.out.println("Prime");
         }
         else
         {
              System.out.println(""); // go to next line
    }

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    prodigytoast (November 5th, 2009)

  4. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Need Help - Factoring & Prime Finding Code

    Thanks alot! That helped me so much, I appreciate it, however, I need to work on the factoring part now, with that current code, it is printing some pretty strange numbers next to the non-prime numbers.
    Ex.
    27 : 39
    28 : 24714
    29 : Prime
    30 : 23561015
    31 : Prime
    32 : 24816
    33 : 311
    34 : 217
    35 : 57
    36 : 234691218
    37 : Prime

  5. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need Help - Factoring & Prime Finding Code

    ahh, forgot... it's not printing out any spaces between the numbers

  6. #5
    Junior Member
    Join Date
    Nov 2009
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Need Help - Factoring & Prime Finding Code

    Oh! haha I didn't realize those were factors! Thanks again!

  7. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Need Help - Factoring & Prime Finding Code

    lol nice one helloworld, always leave something out to confuse them. If it's solved coud you please mark it as so.

    Thanks,
    Chris

Similar Threads

  1. How to find location of ipaddress using Java
    By tej in forum Java Networking
    Replies: 8
    Last Post: September 8th, 2012, 06:05 AM
  2. Replies: 13
    Last Post: May 6th, 2009, 09:27 AM