Skip to main content
added 4 characters in body
Source Link
rnso
  • 1.7k
  • 10
  • 9

Racket 8175 bytes

(let loopp((c 2))(cond[(> c(sqrt n))#t][(= 0(modulo n c))#f][#f][else(loopp(+ 1 c))])) 

Ungolfed:

(define (f n) ; COMMENTS:  (let loop ((c 2)) (cond [(> c (sqrt n)) #t] ; if no divisor found till sq root of number, it must be a prime [(= 0 (modulo n c)) #f] ; if divisor found, it is not a prime [else (loop (add1 c))] ; try with next number ))) 

Following version is longer but more efficient for checking larger numbers since it keeps & uses previously found prime numbers:

(λ(N)(define(p n(o'(2))(c 2))(cond[(> c n)o][(ormap(λ(x)(= 0(modulo c x)))o) (p n o(+ 1 c))][else(p n(cons c o)(+ 1 c))]))(= N(car(p N)))) 

Ungolfed version:

(p=subfunction to build prime number list till n; o= list of prime numbers found; c=current number being checked)

(define f (λ (N) (define (p n (o '(2)) (c 2)) (cond [(> c n) o] [(ormap (lambda(x) (= 0 (modulo c x))) o) (p n o (add1 c)) ] [else (p n (cons c o) (add1 c))])) (= N (first (p N))))) 

Testing:

(f 109) (f 10) (f 11) (f 12) (f 13) (f 49) (f 43) (f 57) (f 47) 

Output:

#t #f #t #f #t #f #t #f #t 

Racket 81 bytes

(let loop((c 2))(cond[(> c(sqrt n))#t][(= 0(modulo n c))#f][(loop(+ 1 c))])) 

Ungolfed:

(define (f n) (let loop ((c 2)) (cond [(> c (sqrt n)) #t] [(= 0 (modulo n c)) #f] [else (loop (add1 c))] ))) 

Following version is longer but more efficient for checking larger numbers since it keeps & uses previously found prime numbers:

(λ(N)(define(p n(o'(2))(c 2))(cond[(> c n)o][(ormap(λ(x)(= 0(modulo c x)))o) (p n o(+ 1 c))][else(p n(cons c o)(+ 1 c))]))(= N(car(p N)))) 

Ungolfed version:

(p=subfunction to build prime number list till n; o= list of prime numbers found; c=current number being checked)

(define f (λ (N) (define (p n (o '(2)) (c 2)) (cond [(> c n) o] [(ormap (lambda(x) (= 0 (modulo c x))) o) (p n o (add1 c)) ] [else (p n (cons c o) (add1 c))])) (= N (first (p N))))) 

Testing:

(f 109) (f 10) (f 11) (f 12) (f 13) (f 49) (f 43) (f 57) (f 47) 

Output:

#t #f #t #f #t #f #t #f #t 

Racket 75 bytes

(let p((c 2))(cond[(> c(sqrt n))#t][(= 0(modulo n c))#f][else(p(+ 1 c))])) 

Ungolfed:

(define (f n) ; COMMENTS:  (let loop ((c 2)) (cond [(> c (sqrt n)) #t] ; if no divisor found till sq root of number, it must be a prime [(= 0 (modulo n c)) #f] ; if divisor found, it is not a prime [else (loop (add1 c))] ; try with next number ))) 

Following version is longer but more efficient for checking larger numbers since it keeps & uses previously found prime numbers:

(λ(N)(define(p n(o'(2))(c 2))(cond[(> c n)o][(ormap(λ(x)(= 0(modulo c x)))o) (p n o(+ 1 c))][else(p n(cons c o)(+ 1 c))]))(= N(car(p N)))) 

Ungolfed version:

(p=subfunction to build prime number list till n; o= list of prime numbers found; c=current number being checked)

(define f (λ (N) (define (p n (o '(2)) (c 2)) (cond [(> c n) o] [(ormap (lambda(x) (= 0 (modulo c x))) o) (p n o (add1 c)) ] [else (p n (cons c o) (add1 c))])) (= N (first (p N))))) 

Testing:

(f 109) (f 10) (f 11) (f 12) (f 13) (f 49) (f 43) (f 57) (f 47) 

Output:

#t #f #t #f #t #f #t #f #t 
added 305 characters in body
Source Link
rnso
  • 1.7k
  • 10
  • 9

Racket 13981 bytes

(let loop((c 2))(cond[(> c(sqrt n))#t][(= 0(modulo n c))#f][(loop(+ 1 c))])) 

LongerUngolfed:

(define (f n) (let loop ((c 2)) (cond [(> c (sqrt n)) #t] [(= 0 (modulo n c)) #f] [else (loop (add1 c))] ))) 

Following version is longer but more efficient (forfor checking larger numbers) since it keeps & uses previously found prime numbers:

(λ(N)(define(p n(o'(2))(c 2))(cond[(> c n)o][(ormap(λ(x)(= 0(modulo c x)))o) (p n o(+ 1 c))][else(p n(cons c o)(+ 1 c))]))(= N(car(p N)))) 

Ungolfed version:

(p=subfunction to build prime number list till n; o= list of prime numbers found; c=current number being checked)

(define f (λ (N) (define (p n (o '(2)) (c 2)) (cond [(> c n) o] [(ormap (lambda(x) (= 0 (modulo c x))) o) (p n o (add1 c)) ] [else (p n (cons c o) (add1 c))])) (= N (first (p N))))) 

Testing:

(f 109) (f 10) (f 11) (f 12) (f 13) (f 49) (f 43) (f 57) (f 47) 

Output:

#t #f #t #f #t #f #t #f #t 

Racket 139 bytes

Longer but more efficient (for checking larger numbers) since it keeps & uses previously found prime numbers:

(λ(N)(define(p n(o'(2))(c 2))(cond[(> c n)o][(ormap(λ(x)(= 0(modulo c x)))o) (p n o(+ 1 c))][else(p n(cons c o)(+ 1 c))]))(= N(car(p N)))) 

Ungolfed version:

(p=subfunction to build prime number list till n; o= list of prime numbers found; c=current number being checked)

(define f (λ (N) (define (p n (o '(2)) (c 2)) (cond [(> c n) o] [(ormap (lambda(x) (= 0 (modulo c x))) o) (p n o (add1 c)) ] [else (p n (cons c o) (add1 c))])) (= N (first (p N))))) 

Testing:

(f 109) (f 10) (f 11) (f 12) (f 13) (f 49) (f 43) (f 57) (f 47) 

Output:

#t #f #t #f #t #f #t #f #t 

Racket 81 bytes

(let loop((c 2))(cond[(> c(sqrt n))#t][(= 0(modulo n c))#f][(loop(+ 1 c))])) 

Ungolfed:

(define (f n) (let loop ((c 2)) (cond [(> c (sqrt n)) #t] [(= 0 (modulo n c)) #f] [else (loop (add1 c))] ))) 

Following version is longer but more efficient for checking larger numbers since it keeps & uses previously found prime numbers:

(λ(N)(define(p n(o'(2))(c 2))(cond[(> c n)o][(ormap(λ(x)(= 0(modulo c x)))o) (p n o(+ 1 c))][else(p n(cons c o)(+ 1 c))]))(= N(car(p N)))) 

Ungolfed version:

(p=subfunction to build prime number list till n; o= list of prime numbers found; c=current number being checked)

(define f (λ (N) (define (p n (o '(2)) (c 2)) (cond [(> c n) o] [(ormap (lambda(x) (= 0 (modulo c x))) o) (p n o (add1 c)) ] [else (p n (cons c o) (add1 c))])) (= N (first (p N))))) 

Testing:

(f 109) (f 10) (f 11) (f 12) (f 13) (f 49) (f 43) (f 57) (f 47) 

Output:

#t #f #t #f #t #f #t #f #t 
deleted 3 characters in body
Source Link
rnso
  • 1.7k
  • 10
  • 9

Racket 139 bytes

Longer but more efficient (for checking larger numbers) since it keeps & uses previously found prime numbers:

(λ(N)(define(p n(o'(2))(c 2))(cond[(> c n)o][(ormap(λ(x)(= 0(modulo c x)))o) (p n o(+ 1 c))][else(p n(cons c o)(+ 1 c))]))(= N(car(p N)))) 

Ungolfed version:

(p=subfunction to build prime number list till n; o= list of prime numbers found; c=current number being checked; checked):

(define f (λ (N) (define (p n (o '(2)) (c 2)) (cond [(> c n) o] [(ormap (lambda(x) (= 0 (modulo c x))) o) (p n o (+ 1add1 c)) ] [else (p n (cons c o) (+ 1add1 c))])) (= N (first (p N))))) 

Testing:

(f 109) (f 10) (f 11) (f 12) (f 13) (f 49) (f 43) (f 57) (f 47) 

Output:

#t #f #t #f #t #f #t #f #t 

Racket 139 bytes

Longer but more efficient (for checking larger numbers) since it keeps & uses previously found prime numbers:

(λ(N)(define(p n(o'(2))(c 2))(cond[(> c n)o][(ormap(λ(x)(= 0(modulo c x)))o) (p n o(+ 1 c))][else(p n(cons c o)(+ 1 c))]))(= N(car(p N)))) 

Ungolfed (p=subfunction to build prime number list till n; o= list of prime numbers found; c=current number being checked; ):

(define f (λ (N) (define (p n (o '(2)) (c 2)) (cond [(> c n) o] [(ormap (lambda(x) (= 0 (modulo c x))) o) (p n o (+ 1 c)) ] [else (p n (cons c o) (+ 1 c))])) (= N (first (p N))))) 

Testing:

(f 109) (f 10) (f 11) (f 12) (f 13) (f 49) (f 43) (f 57) (f 47) 

Output:

#t #f #t #f #t #f #t #f #t 

Racket 139 bytes

Longer but more efficient (for checking larger numbers) since it keeps & uses previously found prime numbers:

(λ(N)(define(p n(o'(2))(c 2))(cond[(> c n)o][(ormap(λ(x)(= 0(modulo c x)))o) (p n o(+ 1 c))][else(p n(cons c o)(+ 1 c))]))(= N(car(p N)))) 

Ungolfed version:

(p=subfunction to build prime number list till n; o= list of prime numbers found; c=current number being checked)

(define f (λ (N) (define (p n (o '(2)) (c 2)) (cond [(> c n) o] [(ormap (lambda(x) (= 0 (modulo c x))) o) (p n o (add1 c)) ] [else (p n (cons c o) (add1 c))])) (= N (first (p N))))) 

Testing:

(f 109) (f 10) (f 11) (f 12) (f 13) (f 49) (f 43) (f 57) (f 47) 

Output:

#t #f #t #f #t #f #t #f #t 
edited body
Source Link
rnso
  • 1.7k
  • 10
  • 9
Loading
added 112 characters in body
Source Link
rnso
  • 1.7k
  • 10
  • 9
Loading
Post Undeleted by rnso
Post Deleted by rnso
deleted 5 characters in body
Source Link
rnso
  • 1.7k
  • 10
  • 9
Loading
Source Link
rnso
  • 1.7k
  • 10
  • 9
Loading