Shop OBEX P1 Docs P2 Docs Learn Events
Spinning an example of loops.... — Parallax Forums

Spinning an example of loops....

GadgetmanGadgetman Posts: 2,436
edited 2006-05-12 20:18 in Propeller 1
I have a few programs I like to use to test the efficiency of a programming-language...

One of these is the Sieve of Erastosthenes, which is a simple way of finding Primes.
somewhere on the net said...

The Sieve of Eratosthenes identifies all prime numbers up to a given number n as follows:

1. Write down the numbers 1, 2, 3, ..., n. We will eliminate composites by marking them. Initially all numbers are unmarked.
2. Mark the number 1 as special (it is neither prime nor composite).
3. Set k=1. Until k exceeds or equals the square root of n do this:
-Find the first number in the list greater than k that has
- not been identified as composite. (The very first number
- so found is 2.) Call it m. Mark the numbers

- 2m, 3m, 4m, ...

- as composite. (Thus in the first run we mark all even
numbers greater than 2. In the second run we mark
all multiples of 3 greater than 3.)
- m is a prime number. Put it on your list.
- Set k=m and repeat.

4. Put the remaining unmarked numbers in the sequence on your list of prime numbers.

Now, My old Sinclair ZX spectrum, with a Z80 processor running at 3.5MHz, can find all the primes up to 255 in about 8 seconds. A Comodore 64 is slightly slower, and the Atari ST seems to do similarly.
All these runs a purely interpreted BASIC, so I expected the Propeller to be slightly better...

So I wrote a little program to test it, and ran it at 5MHz, to be fair...
Except...
The program is set to light a LED(on A16) after it finishes, and it lighted it immediately...

For those curious, any place in the array still '0' after finished execution signifies a Prime.

To change how far it searches, change the MaxPrime constant.
(Just be aware that it IS used to create an array, so there are limits to size. would have been nice to have a Bit-size variable... )

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Don't visit my new website...
Sign In or Register to comment.