![]() ![]() The isPrime could be removed as well if it's not needed elsewhere, simply use if(getPrime(i)) instead.I removed the usage of userNumber inside getPrimeList completely, I simply use i instead.You can go even further and make userNumber an argument of getPrime: public boolean getPrime(int userNumber) There are four primes and six composite numbers. Int numRoot = ((int) Math.sqrt(userNumber)) Mathabulous Prime number tester examples Click to use Check First Ten Positive Integers In this example we run primality test on the first ten positive integers and display all the results. More precisely, factors should be reset to 0 at the start of getPrime.Ī better way to go about this is by making x, numRoot, isPrime and factors local variables of getPrime: public boolean getPrime() The problem is that you're using too many instance variables inside getPrime, causing you to unintentionally inherit state from previous iterations. ("\nThe prime numbers from 1 to %d are: \n 2", end) When I use this method and a while loop to get a list of prime numbers from 1 to a large number it keeps returning 24 then 4 then 5.the variable end, in code below is asked for in a prime class runner separately. I have a getPrime method which returns true if the number is prime or false if it is not prime. I need to create a program that can get all the prime numbers from 1 to a large number. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |