Prime numbers

Started by Fritz, March 23, 2010, 02:46:24 PM

Previous topic - Next topic

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:


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:


(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:


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