Problem fixed
Here's a prime # code in java if you care.
[code]
/**
*
*/
/**
* @author Dan
*
*/
import java.util.Scanner;
import java.util.ArrayList;
public class PrimeSieve {
/**
* @param args
*/
public static ArrayList<Integer> findPrimes(int N) {
//TODO Returns an ArrayList of all the prime numbers to N.
int current = 2;
int index = 0;
ArrayList<Integer> array = new ArrayList<Integer>(N);
ArrayList<Integer> notPrime = new ArrayList<Integer>(N);
ArrayList<Integer> result = new ArrayList<Integer>(N);
int i = 0;
for (i = 2; i <= N; i++ ) {
array.add(i);
}
boolean stop = false;
while (stop == false) {
for (int a : array) {
int lastNum =array.get(array.size()-1);
if (notPrime.contains(a) == false && result.contains(a) == false){
current = a;
result.add(current);
notPrime.add(current);
index = current;
break;
}
if (a== lastNum){
stop = true;
}
}
while (index <=(array.size()-1) && stop == false){
index += current;
//System.out.println("Index: " +index);
notPrime.add(index);
}
current = 0;
}
return result;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("Input end Number: ");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
ArrayList<Integer> array = findPrimes(num);
System.out.print("Prime numbers 2 to "+num+": ");
for(int i : array){
System.out.print(i +" ");
}
}
}
[/code]
if (a == lastNum)[b];[/b]{
That semicolon probably should not be there.
shit fuck. I can't believe eclipse didn't catch that. Thank you very much.
It probably didnt catch it as you can actually have
if (a == lastNum);
(same way as you can have while(true); )
Its just an empty statement
Some fun facts about primes: They all come in the form 6k+-1 (after 3), for any number x the max factor you need to check for x to be prime is the sqrt(x), and you only need to check previous primes if you have a complete list.
[code]
import java.util.ArrayList;
public class Main
{
public static void main(String[] args)
{
int max =120;
if(max<2) return;
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(2);
if(max<3)
{
PrintList(list);
return;
}
list.add(3);
int prime = 1,lo=prime*6-1,hi=prime*6+1;
while(lo<=max)
{
if(isPrime(lo,list))
{
list.add(lo);
}
if(hi>max) break;
if(isPrime(hi,list))
{
list.add(hi);
}
prime++;
lo=prime*6-1;
hi=prime*6+1;
}
PrintList(list);
}
public static boolean isPrime(int test,ArrayList<Integer> list)
{
int j=0;
boolean found = false;
do
{
if(test%list.get(j) ==0)
{
found = true;
}
}while(!found && Math.sqrt(test)>list.get(j++) && j < list.size());
return !found;
}
public static void PrintList(ArrayList<Integer> list) {
for (int i : list) {
System.out.println("" + i + ",");
}
}
}
[/code]
Think that should do it.
[b]edit:[/b] Now with easier to read code
Yeah that would be much more efficient.
However my algorithm follows this : [URL]http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes[/URL]
Sieve of Eratosthenes: basically some greek guy found a found to find primes from counting up the next prime number and discounting all that it comes across. So that's what I needed to follow
[QUOTE=BaconMan_lol;28553984]Yeah that would be much more efficient.
However my algorithm follows this : [URL]http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes[/URL]
Sieve of Eratosthenes: basically some greek guy found a found to find primes from counting up the next prime number and discounting all that it comes across. So that's what I needed to follow[/QUOTE]
That is a pretty neat way of doing if it we didn't have computers doing the brunt of the work. My java is so rusty I had to go into netbeans to make sure that ran.
It would be interesting to see what the average run time for very large sets of primes for your method compared to the tried-and-true brute force method.
It sucks. first 1000 is instant....
10000 long wait
100000 good luck
[QUOTE=BaconMan_lol;28554090]It sucks. first 1000 is instant....
10000 long wait
100000 good luck[/QUOTE]
Mine gets to 10000000 in 10 seconds on my computer.
[editline]11th March 2011[/editline]
Excluding the printing.
[QUOTE=bord2tears;28554070]That is a pretty neat way of doing if it we didn't have computers doing the brunt of the work. My java is so rusty I had to go into netbeans to make sure that ran.
It would be interesting to see what the average run time for very large sets of primes for your method compared to the tried-and-true brute force method.[/QUOTE]
Actually, a sieve of Eratosthenes is one of the most effective ways to generate all primes less than or equal to a certain number. A prime checker using the method bord2tears described is better for testing the primality of specific numbers.
The largest known prime number is:
2^(43,112,609) − 1
[QUOTE=yumi_cheese;28646665]The largest known prime number is:
2^(43,112,609) − 1[/QUOTE]
Yeah, mersenne primes are neat.
Sorry, you need to Log In to post a reply to this thread.