Jump to content

Help w/ a Prime Number Generator

Above7heInfluence

So I need to make a prime number generator that has a class and a main class (a tester). It must take in a user inputted integer and print out all the prime numbers up to that number. I have the class and the main class mostly done but I need a little more help and direction. Any help would be greatly appreciated! Here is my class for it. 

/**   this prints out all prime numbers up to a number inputted..   * author */public class PrimeNumbers{   private boolean isPrime = true;   private int current;     public PrimeNumbers()   {       current = 1;   }   /**      Calculates the next prime number.      @return nextPrime the next prime number if the number is prime.   */   public boolean isPrime(int n)   {       if (n <= 1)           return false;       else if (n > 2 || n % 2 == 0)           return false;       for (int i = 3; i * i <=n; i++)           return n% i != 0;       return false;          }   public int nextPrime()   {       do       {current++;}       while (!isPrime(current));       return current;   }     }

and here is the tester for the class. 

import java.util.Scanner;public class PrimeNumbersTester{   public static void main(String[] args)   {      Scanner in = new Scanner(System.in);            System.out.println("Enter an intger: ");      int n = in.nextInt();      PrimeNumbers fg = new PrimeNumbers();      fg.isPrime(n);      System.out.println(fg.nextPrime());   }}

I know i need to have a loop in the tester but im not sure how to go about the loop. Thank you!

i7 3930k @ 4.4Ghz | AMD 7970 @ 1150/1823 | Asus P9X79 Pro | 16GB G.Skill 2133mhz | 2x WD Caviar Black 1TB in RAID 0 & 250GB Samsung Evo SSD | Rosewill Blackhawk Ultra with Side Window | XSPC RX360 Watercooling Kit w/ UV Blue Tubing | Corsair Vengeance K70 Keyboard w/ Corsair M65 Mouse

 

Link to comment
Share on other sites

Link to post
Share on other sites

Here's a program i Did in my comp scie class that determined if an inputted number was prime. you can go from here

/** * Greetings fellow comrade. Velcome to the Prime-inator! */import java.util.*;public class IsPrime{    public static void main(String[] args)    {        Scanner console = new Scanner(System.in);                System.out.println("Welcome to the Prime Number Determinator!");                double n;          // number inputted        double remain=0;   // the remainder when n is divided by integer p        double p = 2;      // the integer divisors of n -- raise until it equals the squareroot of n                System.out.print("Enter a positive integer for prime-ness determination: ");        n = console.nextDouble();                while (p <= Math.pow(n , 0.5)){            remain = n % p;            p++;            if (remain == 0) {                System.out.println("Your number is not prime (composite).");                break;            }        }                if (remain != 0) {             System.out.println("Your number is prime! Congratulations! One virtual cookie for you!");        }    }}

 

 
Post script: how did you do that formatting?
 
I figured it out!

Aesthetics of rigs matter

42

If you're interested, participate in LTT Build Offs

Link to comment
Share on other sites

Link to post
Share on other sites

 

Here's a program i Did in my comp scie class that determined if an inputted number was prime. you can go from here

double n;          // number inputted        double remain=0;   // the remainder when n is divided by integer p        double p = 2;      // the integer divisors of n -- raise until it equals the squareroot of n                System.out.print("Enter a positive integer for prime-ness determination: ");        n = console.nextDouble();                while (p <= Math.pow(n , 0.5)){            remain = n % p;            p++;            if (remain == 0) {                System.out.println("Your number is not prime (composite).");                break;            }        }                if (remain != 0) {             System.out.println("Your number is prime! Congratulations! One virtual cookie for you!");        }
Post script: how did you do that formatting?
 
I figured it out!

 

Okay thank you for your post! I just don't understand why it isnt running my methods in the tester class...my teacher wants me to set it up like that but i dont get why it isnt running the methods in the tester...>.<

i7 3930k @ 4.4Ghz | AMD 7970 @ 1150/1823 | Asus P9X79 Pro | 16GB G.Skill 2133mhz | 2x WD Caviar Black 1TB in RAID 0 & 250GB Samsung Evo SSD | Rosewill Blackhawk Ultra with Side Window | XSPC RX360 Watercooling Kit w/ UV Blue Tubing | Corsair Vengeance K70 Keyboard w/ Corsair M65 Mouse

 

Link to comment
Share on other sites

Link to post
Share on other sites

Okay thank you for your post! I just don't understand why it isnt running my methods in the tester class...my teacher wants me to set it up like that but i dont get why it isnt running the methods in the tester...>.<

nextPrime is an infinite loop

 

Because this is always returning false due to n > 2

else if (n > 2 || n % 2 == 0)    return false;
Link to comment
Share on other sites

Link to post
Share on other sites

nextPrime is an infinite loop

How would i change it to fix that? Thank you

i7 3930k @ 4.4Ghz | AMD 7970 @ 1150/1823 | Asus P9X79 Pro | 16GB G.Skill 2133mhz | 2x WD Caviar Black 1TB in RAID 0 & 250GB Samsung Evo SSD | Rosewill Blackhawk Ultra with Side Window | XSPC RX360 Watercooling Kit w/ UV Blue Tubing | Corsair Vengeance K70 Keyboard w/ Corsair M65 Mouse

 

Link to comment
Share on other sites

Link to post
Share on other sites

Edited my post to explain

Okay so should i just take out the n > 2? And I have an issue implementing the loop in my tester, why is this giving me an error? 

import java.util.Scanner;public class PrimeNumbersTester{   public static void main(String[] args)   {      Scanner in = new Scanner(System.in);            System.out.println("Enter an integer: ");      int n = in.nextInt();      PrimeNumbers fg = new PrimeNumbers();      while (int i = 1; int i<n; i++))      {           fg.isPrime(n);           System.out.println(fg.nextPrime());      }   }}

Thank you!!!

i7 3930k @ 4.4Ghz | AMD 7970 @ 1150/1823 | Asus P9X79 Pro | 16GB G.Skill 2133mhz | 2x WD Caviar Black 1TB in RAID 0 & 250GB Samsung Evo SSD | Rosewill Blackhawk Ultra with Side Window | XSPC RX360 Watercooling Kit w/ UV Blue Tubing | Corsair Vengeance K70 Keyboard w/ Corsair M65 Mouse

 

Link to comment
Share on other sites

Link to post
Share on other sites

Okay so should i just take out the n > 2?

That'll stop the infinite loop, but the algorithm is still incorrect.

 

 

And I have an issue implementing the loop in my tester, why is this giving me an error?

Your while loop has syntax errors. int i<n doesn't need the int and there's an extra closing bracket.

Link to comment
Share on other sites

Link to post
Share on other sites

That'll stop the infinite loop, but the algorithm is still incorrect.

 

 

Your while loop has syntax errors. int i<n doesn't need the int and there's an extra closing bracket.

Okay what will correct the algorithm? Sorry, that im asking so many questions :(

i7 3930k @ 4.4Ghz | AMD 7970 @ 1150/1823 | Asus P9X79 Pro | 16GB G.Skill 2133mhz | 2x WD Caviar Black 1TB in RAID 0 & 250GB Samsung Evo SSD | Rosewill Blackhawk Ultra with Side Window | XSPC RX360 Watercooling Kit w/ UV Blue Tubing | Corsair Vengeance K70 Keyboard w/ Corsair M65 Mouse

 

Link to comment
Share on other sites

Link to post
Share on other sites

Okay so i got the program to print out prime numbers but it doesnt stop at the number I input? For instance, I input 10 and it prints out to 23 but it should really stop at 7 (since it cant go past 10) ?? here is my tester. 

import java.util.Scanner;public class PrimeNumbersTester{   public static void main(String[] args)   {      Scanner in = new Scanner(System.in);            System.out.println("Enter an integer: ");      int n = in.nextInt();      PrimeNumbers fg = new PrimeNumbers();      int i = 1;      System.out.println("1");      while(i<=n)      {           fg.isPrime(n);           System.out.println(fg.nextPrime());           i++;      }   }}

i tried to put "i++" in the while loop but it gives me an error when i do " while(i<n; i++) " i get an error. 

i7 3930k @ 4.4Ghz | AMD 7970 @ 1150/1823 | Asus P9X79 Pro | 16GB G.Skill 2133mhz | 2x WD Caviar Black 1TB in RAID 0 & 250GB Samsung Evo SSD | Rosewill Blackhawk Ultra with Side Window | XSPC RX360 Watercooling Kit w/ UV Blue Tubing | Corsair Vengeance K70 Keyboard w/ Corsair M65 Mouse

 

Link to comment
Share on other sites

Link to post
Share on other sites

You'll need to provide the PrimeNumbers code as well

I actually figured it out and used my own algorithm for the isPrime method. Thank you for all your help, I guess I easily doubt myself since I'm a beginner and don't understand things very thoroughly yet, thanks again. Glad I can come here for help and get solid help from you and others!!! :)

i7 3930k @ 4.4Ghz | AMD 7970 @ 1150/1823 | Asus P9X79 Pro | 16GB G.Skill 2133mhz | 2x WD Caviar Black 1TB in RAID 0 & 250GB Samsung Evo SSD | Rosewill Blackhawk Ultra with Side Window | XSPC RX360 Watercooling Kit w/ UV Blue Tubing | Corsair Vengeance K70 Keyboard w/ Corsair M65 Mouse

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×