Project Euler #7 Optimization - java

I've been noticing a theme with some of the Project Euler problems. They require a huge amount of time to complete. (Problems like Largest Palindrome and 10,001st Prime) I was hoping there could be some way to optimize the code to make it better. I let this one run for over 24 minutes and it didn't finish.
public class Seven
{
public static void main(String[] args)
{
//Declare Variables
int primeCount = 0;
int numCount = 1;
int latestPrime = 0;
while(primeCount <= 10001)
{
if(isPrime(numCount))
{
primeCount++;
latestPrime = numCount;
}
numCount++;
}
System.out.println("The 10,001st prime is: " + latestPrime);
}
//This method will determine if a number is prime
public static boolean isPrime(long num)
{
//Check for even number
if(num % 2 == 0)
return false;
//Check for non-prime odd numbers
for(long i = 3; i <= num; i += 2)
{
if(num % i == 0)
return false;
}
//Return that the number is prime
return true;
}
}

You're doing a lot of repetitions, you don't need to loop through entire number in the for loop, just go until square root.
This runs in a second.
// Check for non-prime odd numbers
for (long i = 3; i <= Math.sqrt(num) +1; i += 2) {
if (num % i == 0)
return false;
}
If you want to optimize even more, you could store prime numbers in an array and only check if the number divisible by the previous prime numbers.

This program prints 10001 first prime numbers. As #Tejash Desai suggested, 2 optimizations could be done:
(1) keep the found prime numbers in a list so that tests of new primes needs to be done only on items inside this list, rather than all odd numbers; and
(2) test againts factors only up to square root of a number.
void printPrimes() {
// Find first 10001 primes
int p = 0;
for (var i = 2; i <= 10001; i++) {
p = nextPrime();
Console.WriteLine("Prime #{0}: {1}", i, p);
}
}
// Set of known primes
List<int> primes = new List<int>() {2};
// Find the prime next to the last one that was found
int nextPrime() {
int k = primes[primes.Count - 1] + 1;
while (!testPrimeUsingKnownSetOfPrimes(k)) {
k = k + 1;
}
primes.Add(k);
return k;
}
// Check if a number is prime, using the set of known primes
bool testPrimeUsingKnownSetOfPrimes(int n) {
foreach (var p in primes) {
// Largest prime factor of a number can't be greater than square root of the number
if (p > Math.Sqrt(n)) {
return true;
}
if (n % p == 0) {
return false;
}
}
return true;
}
PS Written in C#.

Loop only till less than or equal to the square root of n
//Check for non-prime odd numbers for(long i = 3; i <= Math.sqrt(num); i += 2) { if(num % i == 0) return false; }
Also, another optimization would be to keep storing all the primes you've come across till now in an ArrayList and then only traverse the loop in that list.
//Check for non-prime odd numbers for(int i = 0; i <= arrayList.size(); i ++) { if(num % arrayList.get(i) == 0) return false; } arrayList.add(num);

Related

Need help calculating the amount of different prime factors of every number up to a given number

I want to calculate the amount of different prime factors of every integer up to n. For example, 12 = 2 * 2 * 3, so it has 2 different prime factors, 2 and 3. I want to store each of these values in an array result[], of size n+1, in which result[i] contains the number of different prime factors of the integer i.
I have to use the following external method in my solution:
List<Integer> getPrimes(int n) {
// create array to determine which numbers are prime
boolean isPrime[] = new boolean[n+1];
for(int i=2; i<=n; i++)
isPrime[i] = true; // assume all are prime, we'll filter below
for(int i=3; i*i<=n; i+=2) {
if (isPrime[i]) { // i is prime, so...
for(int j=i*i; j<=n; j+=i) // remove all its multiples
isPrime[j] = false; // by updating array
}
}
// create list with only the prime numbers
List<Integer> primes = new LinkedList<>();
primes.add(2);
for(int i=3; i<=n; i+=2)
if (isPrime[i])
primes.add(i);
return primes;
}
which uses the Sieve of Eratosthenes to return a List of all prime numbers up to n. This was my solution:
int[] getNumPrimeFactors(int n) {
int[] result = new int[n + 1];
int counter; // counts the number of different prime factors
boolean isPrime = true;
List<Integer> primeList = getPrimes(n);
for (int i = 2; i < result.length; i++) {
counter = 0;
// checks if i is prime
if (i % 2 == 0) {
isPrime = false;
} else {
for (int j = 3; j * j <= i; j += 2) {
if (i % j == 0)
isPrime = false;
}
}
// if i isnt prime, counts how many different prime factors it has
if (!isPrime) {
for (int prime : primeList) {
if (i % prime == 0)
counter++;
}
result[i] = counter;
} else {
result[i] = 1;
}
}
return result;
}
This algorithm produces the correct results, however, I want to be able to test for n <= 5_000_000, and it isn't efficient enough. Is there any way I can improve the code for very large instances of n? Here are some example test results:
Thank you very much for your help :)
This is definitely not the most effective algorithm, but on my old computer (i5 3570K) it works up to 5_000_000 slightly more than 10 seconds. Sum of result array for 5_000_000 is 14838426.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
public static List<Integer> primeFactors(int number) {
int n = number;
List<Integer> factors = new ArrayList<Integer>();
for (int i = 2; i <= n / i; i++) {
while (n % i == 0) {
factors.add(i);
n /= i;
}
}
if (n > 1) {
factors.add(n);
}
return factors;
}
public static void main(String[] args) {
int[] result = new int[5_000_001];
for (int i = 0; i < result.length; i++) {
result[i] = (int) primeFactors(i).stream().distinct().count();
}
System.out.println(Arrays.stream(result).sum());
}
}
One standard "improvement" I often find useful on sieving in this way is set up least prime factors for all the values (instead of just a "true/false" value). Then the "is it prime" decision is a check on whether the least prime factor lpf is less than the value. This is basically nearly as quick as the prime sieve but then gives a more direct route to factorizing numbers in range.
In Python:
lim = 5000000+1
# precalc all least prime factors for composites in range
lpf = [i for i in range(lim)]
for m in range(4,lim,2):
lpf[m] = 2
k = 3
while k*k <= lim:
if lpf[k] == k:
for m in range(k*k, lim, 2*k):
if lpf[m] == m:
lpf[m] = k
k += 2
print('lpf done',lim) ############
# find number of distinct prime factors for each
result = [0]*lim
for a in range(2,lim):
pf = lpf[a]
fc = 1
res = a
while pf < res:
res //= pf
if pf != lpf[res]:
fc += 1
pf = lpf[res]
result[a] = fc
print(result[:11])
print(sum(result[:10+1]))
print(sum(result[:1234+1]))
print(sum(result[:1000000+1]))
The sieve here takes just over two seconds, which is about a quarter of the total time.

Algorithm to find prime numbers with odd digits in large range

Given a range of [1, 1000000000] we need to find prime numbers and all the digits of the prime number must be odd. (Example: 23 is not okay, 31 is okay)
If we go on by looping through each number and checking if it is prime etc, it is very slow. Is there a way to make this close to O(N) ?
I tried to eliminate as much as possible by looking at digits first. But after eliminating numbers with even digits the prime test is too slow.
for all numbers in [1, N]
check all digits, if any even digit, continue
check primality (this step is very slow)
And the primality test should not be very complex (probabilistic etc. is not possible, it must be possible to implement in a few minutes). The one I use is:
private static boolean isPrime(int n) {
boolean isPrime = true;
for (int divisor = 2; divisor <= n / 2; divisor++) {
if (n % divisor == 0) {
isPrime = false;
break;
}
}
return isPrime;
}
Maybe there is a trick to ensure a quick primality test but I couldn't find. Any suggestions? Thanks for reading.
You don't need to check all milliard numbers. Generate all numbers with only odd digits - there are at most 5^9~2 millions of them. Exclude those ending with 5 and not generate numbers divisible by 3 (in the moment of the last digit generation)
Then check these numbers for primality. Note that loop limit might be sqrt(n)
Ideone
class Ideone
{
static int oddcnt;
public static void checkprime(int x) {
for (int i=3; i <= Math.sqrt(x); i +=2)
if ((x % i) == 0)
return;
oddcnt++;
}
public static void genodd(int x, int curlen, int maxlen) {
x *= 10;
for (int i=1; i<10; i+=2) {
int nx = x + i;
checkprime(nx);
if (curlen < maxlen)
genodd(nx, curlen + 1, maxlen);
}
}
public static void main (String[] args) throws java.lang.Exception
{
genodd(0, 1, 8);
System.out.println(oddcnt);
}
}
The best way I can think of is to run a Prime Sieve of Eratosthenes to find all the primes in the range (0; sqrt(1000000000)) - which is around (0, 31622) - and time complexity O(n*log(log(n))) where n=31622. We will need those prime for a faster primality test.
Then, just loop through each number with odd digits - there are 5^10 = 9765625 ~ 10000000 such numbers. You saved 1000 times compared to iterating through all number in the original range.
The primality test using the primes we found in step 1 can be fast, as you only need to check with primes < sqrt(n), and you already have the primes. Even for the largest number in the range which is 999999999, the number of candidate primes is just 3432.
The following is a Java implementation
public class Execute {
private ArrayList<Long> primes = new ArrayList<>();
#org.junit.Test
public void findOddDecimalPrimes() {
primeSieve(32000);
System.out.println(primes.size());
for (int i = 0; i < 9765625; i++) {
String inBase5 = convertFromBaseToBase(i);
long evenDec = convertToOddDecimal(inBase5);
if (isPrime(evenDec)) {
System.out.println(evenDec);
}
}
}
private String convertFromBaseToBase(long i) {
return Long.toString(i, 5);
}
private long convertToOddDecimal(String str) {
StringBuilder s = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
s.append(1 + 2 * Integer.parseInt("" + str.charAt(i)));
}
return Long.parseLong(s.toString());
}
private boolean isPrime(long n) {
for (int i = 0; i < primes.size(); i++) {
if (primes.get(i) * primes.get(i) > n) break;
long divisor = n / primes.get(i);
if (divisor * primes.get(i) == n) return false;
}
return true;
}
/**
* References: www.geeksforgeeks.org
*/
private void primeSieve(int n)
{
// Create a boolean array "prime[0..n]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
boolean prime[] = new boolean[n+1];
for(int i=0;i<n;i++)
prime[i] = true;
for(int p = 2; p*p <=n; p++)
{
// If prime[p] is not changed, then it is a prime
if(prime[p] == true)
{
// Update all multiples of p
for(int i = p*p; i <= n; i += p)
prime[i] = false;
}
}
for (int i = 2; i < prime.length; i++) {
if (prime[i]) this.primes.add(Long.valueOf(i));
}
}
}
If your numbers are in order, you can optimize your isPrime function.
Here is a js sample version.
var primes = [];
function checkDigits(n) {
while(n > 1) {
var d = n % 10;
if ( d % 2 == 0) { return false; }
n = parseInt(n/10,10);
}
return true;
}
function isPrime(n) {
for(var i = 1; i < primes.length; i++) {
if(n % primes[i] == 0) {
return false;
}
}
var lastPrime = primes.length > 2 ? primes[primes.length - 1] : 1;
var inc = 2;
for(var i = lastPrime + inc; i < Math.sqrt(n); i += inc) {
if(n % i == 0) {
return false;
}
}
primes.push(n);
return true;
}
for(var i = 1; i < 100; i++) {
if(checkDigits(i) && isPrime(i)) {
console.log(i);
}
}
This is an open question in math and computer science in general.
In a nutshell no, there is no know way to solve that problem in O(1) to get your loop running in O(N) over the whole range.
If you solve that, dont tell anyone, and go get rich by breaking most of the encryptions today that use large prime numbers.
What you could do though, is make the loop over the devisor a bit smaller by useing sqrt(n).
That will bring that inner loop down from O(N^2) to O(sqrt(N))
And the whole complexity from O(N^2) to O(N*sqrt(N))=O(N^(3/2))
Anotger optimization would be to check the odd digits first beforre doing the complex Prime calculation

How to calculate if the sum of integers from 1 to X are prime

A while back I was given a problem that looked something like this:
A lucky number is a number where the sum of the numbers before it (including itself) is prime. For example, two is a lucky number because it becomes 1 + 2. Since this is 3, and 3 is a prime number, we can conclude that 2 is a lucky number.
I've tried constructing a simple code to find a few lucky numbers
public static void main(String args[]) {
boolean isPrime = true;
for(int x = 3; x < 1000; x++) {
int num = ( (x * (x + 1)) / 2);
// formula for checking the sum of integers one through x
for(int i = 2; i < num; i++) {
if(num % i == 0) {
isPrime = false;
// if it's false it is not prime
}
if(isPrime == true) {
System.out.println(x);
// prints out the original number, the sum of the numbers before it is a prime number
}
}
}
}
In other words, it should take a number, x, plug it into the formula and see if the number returned by the formula is a prime number. However, I'm getting long strings of numbers that aren't prime (like 3, 4, 5, 6, 7...).
The sum of integers from 1 to x is x*(x+1)/2.
Either x or x+1 is even, so the sum is always the product of 2 integers -- either x*((x+1)/2) or (x/2)*(x+1).
A prime cannot be made as the product of two integers unless those two integers are 1 and itself, so, if x is "lucky", then either (x+1)/2==1 or x/2==1 or x+1==1 or x==1, i.e., x is either 0, 1 or 2.
Since 0 doesn't count...
1 and 2 are the only lucky numbers
Just a small logical issue. You should print a prime when it is a prime in deed. It means that it doesn't divide by all of those numbers. So move it outside the loop.
for(int x = 3; x < 1000; x++) {
int num = ( (x * (x + 1)) / 2);
// formula for checking the sum of integers one through x
if (num%2 == 0) {
isPrime = false;
}
for(int i = 3; isPrime && i < num ; i += 2) {
if(num % i == 0) {
isPrime = false;
// if it's false it is not prime
}
}
if(isPrime == true) {
System.out.println(x);
// prints out the original number, the sum of the numbers before it is a prime number
}
}
I also added some optimisation - no need to check if n%4, n%6 itd is 0, when it's not even as well as checking if divides by large numbers when already found one divisor.
On the other hand it's impossible for x > 2. The only x for which it works is 2.
At every step of your inner for loop you are checking if it is prime. Because it defaults as being prime, if it isn't found not prime in the first check it shows.
public static void main(String args[]) {
boolean isPrime = true;
for (int x = 3; x < 1000; x++) {
int num = ((x * (x + 1)) / 2);
// formula for checking the sum of integers one through x
for (int i = 2; i < num; i++) {
if (num % i == 0) {
isPrime = false;
// if it's false it is not prime
}
}
if (isPrime == true) {
System.out.println(x);
// prints out the original number, the sum of the numbers before it is a prime number
}
}
it's impossible. But here is my solution using streams!!
public class Main {
public static void main(String[] args) {
System.out.println(
IntStream.range(2, 1000000)
.map(e->factor(e))
.filter(e -> isPrime(e))
.boxed()
.collect(Collectors.toCollection(ArrayList::new))
);
}
public static boolean isPrime(int input) {
for (int i=2;i<input;i++) {
if (input%i==0) {
return false;
}
}
return true;
}
public static int factor(int input) {
return ( (input * (input + 1)) / 2);
}
}

Next Prime number Java only working with certain numbers

This function its only working for certain numbers, but for 15, or 5 it does not give me correct next prime.
public static int nextPrime(int n) {
boolean isPrime = false;
int m = (int) Math.ceil(Math.sqrt(n));
int start = 3;
if (n % 2 == 0) {
n = n + 1;
}
while (!isPrime) {
isPrime = true;
for (int i = start; i <= m; i = i + 2) {
if (n % i == 0) {
isPrime = false;
break;
}
}
if (!isPrime) {
n = n + 2;
}
}
return n;
}
You don't need to go upto sqrt(n), you need to go upto sqrt(number) that you are evaluating
for example consider you pass n = 5
it will start loop from 3 and it will end the loop at 4 that is not what you need to find next prime number
outer loop
start from n + 1 until you find prime
inner loop
you should start from 3 and sqrt(numberUnderIteration)
You're setting your boundary at the square root of the original number only. In order for you to check if every next number works, you need to recalculate the boundary whenever the n value is changed. So, put int m = (int) Math.ceil(Math.sqrt(n)); inside of your while loop.
You also need to increment n by 1 before you start any calculations, or it will accept n itself as a prime number if it is one. For example, nextPrime(5) would return 5 because it passes the conditions.
And finally, you don't need to increment n by 2 at the end of your while loop because if you are on an even number, it will break out (keep adding 2 to an even number will always be even). I've commented the part of your code that I changed:
public static int nextPrime(int n) {
boolean isPrime = false;
int start = 2; // start at 2 and omit your if statement
while (!isPrime) {
// always incrememnt n at the beginning to check a new number
n += 1;
// redefine max boundary here
int m = (int) Math.ceil(Math.sqrt(n));
isPrime = true;
// increment i by 1, not 2 (you're skipping numbers...)
for (int i = start; i <= m; i++) {
if (n % i == 0) {
isPrime = false;
break;
}
}
// you don't need your "if (!isPrime)..." because you always increment
}
return n;
}
public static void main(String[] args) {
System.out.println(nextPrime(15)); // 17
System.out.println(nextPrime(5)); // 7
System.out.println(nextPrime(8)); // 11
}
You need to compute m inside the for loop.
while (!isPrime) {
isPrime = true;
int m = (int) Math.ceil(Math.sqrt(n));
// do other stuff
Your code works fine except when a prime number is given as input, your method returns input itself.
Example if 5 is your input nextPrime(5) returns 5. If you want 7 (next prime number after 5) to be returned in this case.
Just add n=n+1; at the start of your method. Hope this helps
Just for fun, I coded a quick Prime class that tracks known primes, giving a huge performance boost to finding multiple large primes.
import java.util.ArrayList;
public class Primes {
private static ArrayList<Integer> primes = new ArrayList<Integer>();
public static int nextPrime(int number){
//start it off with the basic primes
if(primes.size() == 0){
primes.add(2);
primes.add(3);
primes.add(5);
primes.add(7);
}
int idx = primes.size()-1;
int last = primes.get(idx);
//check if we already have the prime we are looking for
if(last > number){
//go to the correct prime and return it
boolean high = false;
boolean low = false;
int prevIdx = 0;
int spread = 0;
//keep finagling the index until we're not high or low
while((high = primes.get(idx-1) > number) || (low = primes.get(idx) <= number)){
spread = Math.abs(prevIdx-idx);
//because we always need to move by at least 1 or we will get stuck
spread = spread < 2 ? 2: spread;
prevIdx = idx;
if(high){
idx -= spread/2;
} else if(low){
idx += spread/2;
}
};
return primes.get(idx);
}
/*FIND OUR NEXT SERIES OF PRIMES*/
//just in case 'number' was prime
number++;
int newPrime = last;
//just keep adding primes until we find the right one
while((last = primes.get(primes.size()-1)) < number){
//here we find the next number
newPrime += 2;
//start with the assumption that we have a prime, then try to disprove that
boolean isPrime = true;
idx = 0;
int comparisonPrime;
int sqrt = (int) Math.sqrt(newPrime);
//make sure we haven't gone over the square root limit- also use post-increment so that we use the idx 0
while((comparisonPrime = primes.get(idx++)) <= sqrt){
if(newPrime % comparisonPrime == 0){
isPrime = false;
}
}
if(isPrime){
primes.add(newPrime);
}
}
return last;
}
}
And here is the test:
public class Test {
public static void main(String[] args){
long start;
long end;
int prime;
int number;
number = 1000000;
start = System.currentTimeMillis();
prime = Primes.nextPrime(number);
end = System.currentTimeMillis();
System.out.println("Prime after "+number+" is "+prime+". Took "+(end-start)+" milliseconds.");
number = 500;
start = System.currentTimeMillis();
prime = Primes.nextPrime(number);
end = System.currentTimeMillis();
System.out.println("Prime after "+number+" is "+prime+". Took "+(end-start)+" milliseconds.");
number = 1100000;
start = System.currentTimeMillis();
prime = Primes.nextPrime(number);
end = System.currentTimeMillis();
System.out.println("Prime after "+number+" is "+prime+". Took "+(end-start)+" milliseconds.");
}
}
This results in the following output:
Prime after 1000000 is 1000003. Took 384 milliseconds.
Prime after 500 is 503. Took 10 milliseconds.
Prime after 1100000 is 1100009. Took 65 milliseconds.
As you can see, this takes a long time the first iteration, but we only have to perform that operation once. After that, our time is cut down to almost nothing for primes less than our first number (since it's just a lookup), and it is very fast for primes that are just a bit bigger than our first one (since we have already done most of the work).
EDIT: Updated search for existing primes using a variation on a Binary Search Algorithm. It cut the search time at least in half.
import java.util.Scanner;
class Testing
{
public static void main(String Ar[])
{
int a = 0, i, j;
Scanner in = new Scanner(System.in);
a = in.nextInt();
for (j = a + 1;; j++)
{
for (i = 2; i < j; i++)
{
if (j % i == 0)
break;
}
if (i == j)
{
System.out.println(j);
break;
}
}
}
}
Here is the perfect code for finding next prime for a given number.
public class NextPrime
{
int nextPrime(int x)
{
int num=x,j;
for( j=num+1;;j++)
{
int count=0;
for(int i=1;i<=j;i++)
{
if(j%i==0)
{
count++;
//System.out.println("entered");
}
//System.out.println(count);
}
if(count==2)
{
System.out.println(" next prime is ");
break;
}
}return j;
}
public static void main(String args[])
{
NextPrime np = new NextPrime();
int nxtprm = np.nextPrime(9);
System.out.println(nxtprm);
}
}
//I hope the following code works exactly.
import java.util.Scanner;
public class NextPrime {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a positive integer number : ");
int n = scanner.nextInt();
for (int x = n + 1;; x++) {
boolean isPrime = true;
for (int i = 2; i < x / 2; i++) {
if (x % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.println("Next prime is : " + x);
break;
}
}
}
}

Can find all the prime numbers but doesn't include "2"?

It now can find all the prime numbers in the input range, but it can't find number 2, the smallest prime number.
for(int number=2;number<range;number++){
for(int testDivide=2;testDivide<Math.sqrt(number);testDivide++){
if(number%testDivide!=0) {
System.out.println(number);
}
break;
}
For range 10 it prints:
5
7
9
but no 2.
The reason your code is not producing correct results (missing 2 and 3; including 9) is that your primality test logic is backwards. A number is prime if the inner loop completes without finding any even divisors; instead you are printing the number if you find any non-divisor.
Try this instead:
for( int number = 2; number < range; number++) {
boolean divisible = false;
int limit = (int) Math.sqrt(number);
for (int testDivide = 2; !divisible && testDivide <= limit; testDivide++) {
divisible = number % testDivide == 0;
}
if (!divisible) {
System.out.println(number);
}
}
Note that a much more efficient way to generate all primes in a range is the Sieve of Eratosthenes.
check the code here:
package core;
public class Test2 {
public static void main(String[] args) {
int cnt = 0;
for (int i = 2;; i++) {
if (Priem(i)) {
cnt++;
System.out.println(i);
if (cnt == 200)
break;
}
}
}
public static boolean Priem(int n) {
for (int i = 2; i < n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}
Note that one of the best ways to generate a list of prime numbers is the "Sieve of Erwhatshisface":
Create a list of consecutive integers starting with 2, up to the max number you'd like to search. Take the first non-zero number in the list (2) and repeatedly step 2 from that location, zeroing out every 2nd list element.
Next take the second non-zero number (3) and repeatedly step 3 from that location, zeroing. Continue with each non-zero value in the list, until you've processed all of them (or at least halfway through, at which point you'll be stepping beyond the end of the list).
The non-zero numbers remaining are all primes.
Reposting code here as not fit in comments:
public static void main(String[] args) {
int cnt = 0;
for (int i = 2;; i++) {
if(i==2){
System.out.println(i);
continue;
}
if (Priem(i)) {
cnt++;
System.out.println(i);
if (cnt == 200)
break;
}
}
}
public static boolean Priem(int n) {
for (int i = 2; i <Math.sqrt(n)+1; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
I think you have your for loop a little mixed up.
for(int testDivide=2;testDivide<Math.sqrt(number);testDivide++){
}
Not sure why you are stopping when testDivide is equal to sqrt(number), you should stop when testDivide is greater than.
Also inside your inner for loop isn't correct either:
if(number%testDivide!=0) {
System.out.println(number);
}
break;
Basically all this will do is check to see of the number is divisible by 2 and then break. You only need to break when you find a number which cleanly divides (number%testDivide==0). Maybe keep a boolean which you set to true when you break and only print after the inner for loop finishes if that boolean is false.
Something along the lines:
for (int number=2; number<range; number++){
boolean found = false;
int limit = (int)Math.sqrt(number);
for (int testDivide=2; testDivide<=limit; testDivide++){
if(number%testDivide==0) {
found = true;
break;
}
}
if (!found) System.out.println(number);
}
In your code when number is 2, sqrt(2) is 1.41 and control doesn't go into the loop. I didn't get the logic behind iterating upto sqrt(number). Try this code
public class Test {
public static void main(String[] args) {
int range = 500; //I assume
for (int i = 2; i< range; i++) {
if (isPrime(i)) {
System.out.println(i);
}
}
}
public static boolean isPrime(int number) {
for (int i = 2; i <= number/2; i++) {
if (number % i == 0) {
return false;
}
if(i % 2 == 1) {
i++; //If not divided by 2 then
// need not to check for any even number
// Essentially incrementing i twice hereafter
}
}
return true;
}
}

Categories

Resources