Prime numbers
Posted: Tue Mar 23, 2010 9:46 pm
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:
I have tried to translate this program to newLISP:
My code is as short as Basic one, but it is still 20% slower than a sample code from newLISP manual:
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
Code: Select all
(define (prime? x) (= (first (factor x)) x))
(println (filter prime? (sequence 2 100)))
Code: Select all
(define (primes n , p)
(dotimes (e n)
(if (= (length (factor e)) 1)
(push e p -1))) p)