Prime Numbers

Tell us about your projects. Update us regularly.
Post Reply
parkview
Guru Maker
Posts: 603
Joined: Tue Jun 24, 2014 8:25 pm
Location: Busselton
Contact:

Prime Numbers

Post by parkview » Mon Feb 16, 2015 9:39 pm

Did you read today's Slashdot article about the NIM programming language: http://developers.slashdot.org/story/15 ... g-traction I bit, because i don't remember hearing about NIM before. Reading the 'Whats special about NIM' page, near the bottom it had a speed comparison of Nim, C and Python all generating Prime Numbers. The python example didn't import any modules, so i tried out the code on my RPi B. It took 52 seconds to calculate up to 3137 primes:

Code: Select all

def eratosthenes(n):
  old = 0
  sieve = [1] * 2 + [0] * (n - 1)
  for i in range(int(n**0.5)):
    if not sieve[i]:
      for j in range(i*i, n+1, i):
        sieve[j] = 1
        if i != old:
          old = i
          print i, " ",
  return sieve

eratosthenes(1000000)


I added a bit to print out the Prime numbers. But, where they actually prime numbers? Lets segway back to 1995-6...

Back then there was a Windows programming language called Delphi and it was all the rage. A few of us (hello Stephen) started the Busselton Delphi User Group (BDUG) and we would try and meet up monthly. One project I did was to write a simple Prime number program to help benchmark the early PC's:

PrimeNumber1.jpg
Delphi based Prime Number Generator
PrimeNumber1.jpg (76.69 KiB) Viewed 2910 times


In the above screenshot, I have opened up one of the menu items (RH window) that listed some of the previous historical benchmarks! On the LH side, I show the current speed to generate the list of Primes up to 37,000 on my 3.5yo (CPU: i5-2540M - 2.6GHz) laptop. It managed it in 0.172 seconds.

The obligatory About window:

about.jpg
About
about.jpg (13.29 KiB) Viewed 2910 times


Whoops, that email address won't work!

LOL - how times change :D

Yes, comparing the two lists show that the above python code was generating Prime numbers, just very slowly! Also, for some reason, even with the large 10,000,000 number, it only spat out 3137 prime numbers and it took around 47.9 seconds (time varies a bit) to do so.

At least it's a LOT faster than my first computer: a 1979 TRS-80 Model 1, running at 1.7MHz. That was my first go at writing a Prime Number generator. I first wrote it in BASIC. I don't remember whether it was searching of 1000, or 10,000, but I manually drew a graph on paper listing various results, and I worked out it was going to take a bout a week to reach the goal I had set it. I think that might have set me off on my first foray into writing Z-80 assembly code. Yep, it was much faster and spat out the answer in a few hours. I was very impressed.

Footnote: So, what happened to the BDUG group you ask? It lasted for a few years and then morphed into a more exiting Linux user group which lasted until around 2003 when StephenE and myself coincidently both sold our respective businesses at the same time and moved onto other ventures. I think the Wayback Machine once had a copy of one of the 1998? Publisher based webpages I posted about the groups meetings.

Post Reply