473,396 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Help with algorithm of Sieve of Eratosthenes

Hey everyone....
I would appreciate any type of help if anyone could explain me how to
translate this algorithm to Visual Basic, im still learning and i
would appreciate this algorithm to my prime number program, which
lists primes but can use different algorithms.

// arbitrary search limit
limit ← 1.000.000

// assume all numbers are prime at first
is_prime(i) ← true, i ∈ [2, limit]

for n in [2, √limit]:
if is_prime(n):
// eliminate multiples of each prime,
// starting with its square
is_prime(i) ← false, i ∈ {n², n²+n, n²+2n, ..., limit}

for n in [2, limit]:
if is_prime(n): print n

this is listed in wikipedia, and there is also another form which i
find even more complicated. but i dont understand how can i eliminate
multiples and all of the rest, if anyone could point me to an easy
explanation.

Thanks in advance...

Apr 10 '07 #1
4 3379
On Apr 10, 10:05 am, "knu...@gmail.com" <knu...@gmail.comwrote:
Hey everyone....
I would appreciate any type of help if anyone could explain me how to
translate this algorithm to Visual Basic, im still learning and i
would appreciate this algorithm to my prime number program, which
lists primes but can use different algorithms.

// arbitrary search limit
limit ← 1.000.000

// assume all numbers are prime at first
is_prime(i) ← true, i ∈ [2, limit]

for n in [2, √limit]:
if is_prime(n):
// eliminate multiples of each prime,
// starting with its square
is_prime(i) ← false, i ∈ {n², n²+n, n²+2n, ..., limit}

for n in [2, limit]:
if is_prime(n): print n

this is listed in wikipedia, and there is also another form which i
find even more complicated. but i dont understand how can i eliminate
multiples and all of the rest, if anyone could point me to an easy
explanation.

Thanks in advance...
http://www.physicsforums.com/showthread.php?t=9298

Let me know if that thread doesn't answer your question and I'll take
another look.

Thanks,

Seth Rowe

Apr 10 '07 #2
On Apr 10, 3:46 pm, "rowe_newsgroups" <rowe_em...@yahoo.comwrote:
On Apr 10, 10:05 am, "knu...@gmail.com" <knu...@gmail.comwrote:
Hey everyone....
I would appreciate any type of help if anyone could explain me how to
translate this algorithm to Visual Basic, im still learning and i
would appreciate this algorithm to my prime number program, which
lists primes but can use different algorithms.
// arbitrary search limit
limit ← 1.000.000
// assume all numbers are prime at first
is_prime(i) ← true, i ∈ [2, limit]
for n in [2, √limit]:
if is_prime(n):
// eliminate multiples of each prime,
// starting with its square
is_prime(i) ← false, i ∈ {n², n²+n, n²+2n, ..., limit}
for n in [2, limit]:
if is_prime(n): print n
this is listed in wikipedia, and there is also another form which i
find even more complicated. but i dont understand how can i eliminate
multiples and all of the rest, if anyone could point me to an easy
explanation.
Thanks in advance...

http://www.physicsforums.com/showthread.php?t=9298

Let me know if that thread doesn't answer your question and I'll take
another look.

Thanks,

Seth Rowe
If anyone else could give another hint, all things that are said in
that forum are kinda advanced to me..its all about datasets and
statusbars...i just wantend something to print prime numbers in a
textbox, using the sieve of eratosthenes...

thanks

Apr 10 '07 #3
On Apr 10, 12:57 pm, "knu...@gmail.com" <knu...@gmail.comwrote:
On Apr 10, 3:46 pm, "rowe_newsgroups" <rowe_em...@yahoo.comwrote:
On Apr 10, 10:05 am, "knu...@gmail.com" <knu...@gmail.comwrote:
Hey everyone....
I would appreciate any type of help if anyone could explain me how to
translate this algorithm to Visual Basic, im still learning and i
would appreciate this algorithm to my prime number program, which
lists primes but can use different algorithms.
// arbitrary search limit
limit ← 1.000.000
// assume all numbers are prime at first
is_prime(i) ← true, i ∈ [2, limit]
for n in [2, √limit]:
if is_prime(n):
// eliminate multiples of each prime,
// starting with its square
is_prime(i) ← false, i ∈ {n², n²+n,n²+2n, ..., limit}
for n in [2, limit]:
if is_prime(n): print n
this is listed in wikipedia, and there is also another form which i
find even more complicated. but i dont understand how can i eliminate
multiples and all of the rest, if anyone could point me to an easy
explanation.
Thanks in advance...
http://www.physicsforums.com/showthread.php?t=9298
Let me know if that thread doesn't answer your question and I'll take
another look.
Thanks,
Seth Rowe

If anyone else could give another hint, all things that are said in
that forum are kinda advanced to me..its all about datasets and
statusbars...i just wantend something to print prime numbers in a
textbox, using the sieve of eratosthenes...

thanks
Here's another article - I believe it uses the Sieve of Eratosthenes:

http://www.codeproject.com/cs/algori...imenumbers.asp

I also took the liberty of translating the code into VB for you - I
didn't test the algorithm completely though so be careful:

Sub Main()

' Change this for a different cutoff
Dim topNumber As Integer = 1000000
Dim numbers As BitArray = New BitArray(topNumber, True)

For i As Integer = 2 To topNumber - 1
If numbers(i) Then
For j As Integer = i * 2 To topNumber - 1 Step i
numbers(j) = False
Next
End If
Next

Dim primes As Integer = 0

For i As Integer = 1 To topNumber - 1
If numbers(i) Then
primes += 1
' Console.WriteLine(i.ToString())
End If
Next

Console.WriteLine()
Console.WriteLine("{0} out of {1} prime numbers found.",
primes, topNumber)
Console.Read()
End Sub

Thanks,

Seth Rowe

Apr 10 '07 #4
On Apr 10, 6:47 pm, "rowe_newsgroups" <rowe_em...@yahoo.comwrote:
On Apr 10, 12:57 pm, "knu...@gmail.com" <knu...@gmail.comwrote:
On Apr 10, 3:46 pm, "rowe_newsgroups" <rowe_em...@yahoo.comwrote:
On Apr 10, 10:05 am, "knu...@gmail.com" <knu...@gmail.comwrote:
Hey everyone....
I would appreciate any type of help if anyone could explain me how to
translate this algorithm to Visual Basic, im still learning and i
would appreciate this algorithm to my prime number program, which
lists primes but can use different algorithms.
// arbitrary search limit
limit ← 1.000.000
// assume all numbers are prime at first
is_prime(i) ← true, i ∈ [2, limit]
for n in [2, √limit]:
if is_prime(n):
// eliminate multiples of each prime,
// starting with its square
is_prime(i) ← false, i ∈ {n², n²+n, n²+2n, ..., limit}
for n in [2, limit]:
if is_prime(n): print n
this is listed in wikipedia, and there is also another form which i
find even more complicated. but i dont understand how can i eliminate
multiples and all of the rest, if anyone could point me to an easy
explanation.
Thanks in advance...
>http://www.physicsforums.com/showthread.php?t=9298
Let me know if that thread doesn't answer your question and I'll take
another look.
Thanks,
Seth Rowe
If anyone else could give another hint, all things that are said in
that forum are kinda advanced to me..its all about datasets and
statusbars...i just wantend something to print prime numbers in a
textbox, using the sieve of eratosthenes...
thanks

Here's another article - I believe it uses the Sieve of Eratosthenes:

http://www.codeproject.com/cs/algori...imenumbers.asp

I also took the liberty of translating the code into VB for you - I
didn't test the algorithm completely though so be careful:

Sub Main()

' Change this for a different cutoff
Dim topNumber As Integer = 1000000
Dim numbers As BitArray = New BitArray(topNumber, True)

For i As Integer = 2 To topNumber - 1
If numbers(i) Then
For j As Integer = i * 2 To topNumber - 1 Step i
numbers(j) = False
Next
End If
Next

Dim primes As Integer = 0

For i As Integer = 1 To topNumber - 1
If numbers(i) Then
primes += 1
' Console.WriteLine(i.ToString())
End If
Next

Console.WriteLine()
Console.WriteLine("{0} out of {1} prime numbers found.",
primes, topNumber)
Console.Read()
End Sub

Thanks,

Seth Rowe
Thank you! This is very helpful.

Apr 10 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: MSiegel | last post by:
hi there! i have to program the sieve of eratosthenes in php as a homework. after i had created an html file where the maximum is set i wrote a php script which doesn't work properly - actually...
0
by: Mark A. Washburn | last post by:
/* SIEVE OF ERATOSTHENES from BYTE magazine -------------------- -- compiled with jdk 1.1.7b with optimize on ( -O) -- Run times on 300 MHz Pentium 2 Windows 95 -- in order of output, from...
32
by: Cmorriskuerten | last post by:
HI, is this is this solution to test if a number is a prime number or not: /* * Is n a prime number? * Return TRUE (1): n is a prime number * Return FALSE (0): n is a *not* a prime number...
32
by: someone else | last post by:
hi all I'm a newbie to this group. my apologies if I break any rules. I've wrote a simple program to find the first 1,000,000 primes, and to find all primes within any range (up to 200 *...
6
by: ckroom | last post by:
Does anyone know a good algorithm to get the next prime of a number n? prime = nextprime( n ); It's for some stuff about double rehashing, thanks. -- ckroom http://nazaries.net/~ckroom
15
by: Steve Bergman | last post by:
Just wanted to report a delightful little surprise while experimenting with psyco. The program below performs astonoshingly well with psyco. It finds all the prime numbers < 10,000,000 ...
9
by: k1ckthem1dget | last post by:
Can someone please help me with this program. How would i go about writing a program which determines the prime numbers from 2 to 1000 in increasing order using the sieve method of Eratosthenes....
2
by: Fabrizio Romano | last post by:
Hello, I have a problem with a generic method. I have written a sieve to generate prime numbers. This method takes an input parameter which is the upperbound of the last prime I need to get. So...
3
by: jzakiya | last post by:
This is to announce the release of my paper "Ultimate Prime Sieve -- Sieve of Zakiiya (SoZ)" in which I show and explain the development of a class of Number Theory Sieves to generate prime...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.