Emirp primes: Difference between revisions

Added a Scheme implementation.
m (→‎{{header|Phix}}: no longer any need to hide command_line() within iff(platform()=JS...))
(Added a Scheme implementation.)
Line 4,047:
Emirps between 7700 and 8000: 7717,7757,7817,7841,7867,7879,7901,7927,7949,7951,7963
10,000 emirp: 948349</pre>
=={{header|Scheme}}==
{{works with|Chez Scheme}}
<lang scheme>; Primality test by simple trial division.
(define prime?
(lambda (num)
(if (< num 2)
#f
(let loop ((div 2))
(cond ((> (* div div) num) #t)
((zero? (modulo num div)) #f)
(else (loop (1+ div))))))))
 
; Check if number is an emirp prime.
(define emirp?
(lambda (num)
(and (prime? num)
(let ((rev (string->number (list->string (reverse (string->list (number->string num)))))))
(and (not (= num rev)) (prime? rev))))))
 
(printf "The first 20 emirps:")
(do ((num 1 (1+ num)) (cnt 0))
((>= cnt 20))
(when (emirp? num)
(set! cnt (1+ cnt))
(printf " ~d" num)))
(newline)
 
(printf "All emirps between 7700 and 8000:")
(do ((num 7700 (1+ num)))
((>= num 8000))
(when (emirp? num)
(printf " ~d" num)))
(newline)
 
(printf "The 10000th emirp: ~d~%"
(do ((num 1 (1+ num)) (cnt 0))
((>= cnt 10000) (1- num))
(when (emirp? num) (set! cnt (1+ cnt)))))</lang>
{{out}}
<pre>The first 20 emirps: 13 17 31 37 71 73 79 97 107 113 149 157 167 179 199 311 337 347 359 389
All emirps between 7700 and 8000: 7717 7757 7817 7841 7867 7879 7901 7927 7949 7951 7963
The 10000th emirp: 948349</pre>
 
=={{header|Sidef}}==