Author Topic: Sieve of Eratosthenes  (Read 637 times)

0 Members and 1 Guest are viewing this topic.

Offline $Clone

  • Peasant
  • *
  • Posts: 86
  • Cookies: 5
  • $---Shadowalker---$
    • View Profile
Sieve of Eratosthenes
« on: October 29, 2015, 07:35:20 pm »
So am studying cryptography and in cryptography we deal with prime numbers and am sure there many way to get prime numbers but i thought i implement Sieve of Eratosthenes wiki https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
Here is my code in python and also Java:

Python sample
Code: (Python) [Select]
print"*****************************************************"
print"\t\tSieve of Eratosthenes"
print"*****************************************************"
num=raw_input("||Enter a number:")
num=int(num)
result=range(2,num)
print"||-----------------------------"
print"||Prime numbers between 0-",num
print"||-----------------------------"

for i in result:
for j in result:
if j!=i and j%i==0:
result.pop(result.index(j))


print "||PRIME NUMBERS:",result
print"||------------------------------"
print"*****************************************************"





Java sample
Code: (Java) [Select]
import java.util.Scanner;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
class PrimeNo{
public static void main(String... args){
int num;
Scanner input=new Scanner(System.in);
System.out.println("***********************************************");
System.out.println("\t\tSieve of Eratosthenes");
System.out.println("***********************************************");
System.out.println("||Find Prime Numbers");
System.out.println("||---------------------------------------------");
System.out.print("||Enter range:");
num=input.nextInt();
List<Integer> range=new CopyOnWriteArrayList<Integer>();
for(int i=2;i<num;i++)
{
range.add(i);
}

for(int i:range)
{
for(int j:range)
{
if(j!=i && j%i==0)

range.remove(range.indexOf(j));


}
}
}
System.out.println("||---------------------------------------------");
System.out.println("||---------------------------------------------");
System.out.println("||PRIME NOs:"+range);
System.out.println("||---------------------------------------------");
System.out.println("||---------------------------------------------");
System.out.println("***********************************************");
System.out.println("***********************************************");
}
}


Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Sieve of Eratosthenes
« Reply #1 on: October 29, 2015, 08:16:57 pm »

Offline $Clone

  • Peasant
  • *
  • Posts: 86
  • Cookies: 5
  • $---Shadowalker---$
    • View Profile
Re: Sieve of Eratosthenes
« Reply #2 on: October 29, 2015, 10:14:53 pm »
We had a challenge on that: https://evilzone.org/weekly-challenge/challenge-17-sieve-of-eratosthenes/
Damn! did see that thread..... nicely  done in c/c++ i guess i don't need to do it again!