Account name:
Password
(OpenID?)
(Forgot it?)
Remember Me
You're viewing
dougo
's journal
Create a Dreamwidth Account
Learn More
Interest
Region
Site and Account
FAQ
Email
Reload page in style:
site
light
Doug Orleans's Journal
Recent Entries
Archive
Reading
Tags
Memories
Profile
no subject
Date:
2008-03-17 06:25 am (UTC)
From:
darius.livejournal.com
FWIW, here's my brute-force solution. I screwed up initially with (- s n) instead of (- n s).
(define (problem-46)
(let loop ((k 3))
(if (or (prime? k)
(sum-of-a-prime-and-twice-a-square? k))
(loop (+ k 2))
k)))
(define (sum-of-a-prime-and-twice-a-square? n)
(any prime? (map (lambda (s) (- n s))
(twice-squares-less-than n))))
(define (twice-squares-less-than n)
(let loop ((k 1) (results '()))
(let ((s (* 2 k k)))
(if (< s n)
(loop (+ k 1) (cons s results))
results))))
(define (prime? n)
(or (= n 2)
(and (not (even? n))
(let testing ((k 3))
(or (< n (* k k))
(and (not (= 0 (remainder n k)))
(testing (+ k 2))))))))
From:
Anonymous
This account has disabled anonymous posting.
OpenID
Identity URL:
Log in?
Dreamwidth account
Account name
Password
Log in?
If you don't have an account you can
create one now
.
Subject
HTML doesn't work in the subject.
Formatting type
Casual HTML
Markdown
Raw HTML
Rich Text Editor
Message
.
Profile
dougo
The Steak Place
Most Popular Tags
#happybirthday
-
1 use
#lj18
-
1 use
#mylivejournal
-
1 use
music
-
1 use
top 10
-
1 use
writer's block
-
1 use
Powered by
Dreamwidth Studios
Style Credit
Style:
Black
for
Negatives
by
phoenix
Expand Cut Tags
No cut tags
no subject
Date: 2008-03-17 06:25 am (UTC)(define (problem-46)
(let loop ((k 3))
(if (or (prime? k)
(sum-of-a-prime-and-twice-a-square? k))
(loop (+ k 2))
k)))
(define (sum-of-a-prime-and-twice-a-square? n)
(any prime? (map (lambda (s) (- n s))
(twice-squares-less-than n))))
(define (twice-squares-less-than n)
(let loop ((k 1) (results '()))
(let ((s (* 2 k k)))
(if (< s n)
(loop (+ k 1) (cons s results))
results))))
(define (prime? n)
(or (= n 2)
(and (not (even? n))
(let testing ((k 3))
(or (< n (* k k))
(and (not (= 0 (remainder n k)))
(testing (+ k 2))))))))