Prime numbers

Pondering the philosophy behind the language
Post Reply
Fritz
Posts: 66
Joined: Sun Sep 27, 2009 12:08 am
Location: Russia

Prime numbers

Post by Fritz »

Found today a story about a boy, who was said by a teacher to print all prime numbers from 2 to 100. He had 2 hours to make a program on Basic. Boy had a working code in ten minutes:

Code: Select all

10 CLS
20 PRINT "2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97"
30 END
I have tried to translate this program to newLISP:

Code: Select all

(define (prime? x) (= (first (factor x)) x))
(println (filter prime? (sequence 2 100)))
My code is as short as Basic one, but it is still 20% slower than a sample code from newLISP manual:

Code: Select all

(define (primes n , p)
  (dotimes (e n) 
    (if (= (length (factor e)) 1) 
      (push e p -1))) p)

Post Reply