patternMinor
Solution to Project Euler #1 - multiples of 3 and 5
Viewed 0 times
projectmultipleseulerandsolution
Problem
I have been programming for about ~2 years, and mostly wrote OOP and structural code. Recently, I have decided to pick up a functional programming language, and Haskell being too alien for me, looked to Racket (since it is high time I learned a LISP anyways) and am loving it. Since this is a new area of programming for me, I would appreciate any feedback you could give me on this program. It is the solution to the first Project Euler program.
`;If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
;Find the sum of all the multiples of 3 or 5 below 1000.
#lang racket
(define max 1000)
(define (multiple_of base test)
(equal? (remainder test base) 0))
(define (primes current total)
(if (
`;If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
;Find the sum of all the multiples of 3 or 5 below 1000.
#lang racket
(define max 1000)
(define (multiple_of base test)
(equal? (remainder test base) 0))
(define (primes current total)
(if (
Solution
- In Scheme, we use hyphens to separate words, not underscores. Also boolean-returning procedures should end in
?. So it should bemultiple-of?
- Instead of
(equal? x 0), use(zero? x).
-
Instead of a recursive loop, you can use
for comprehensions:(for/sum ((i (in-range 1000))
#:when (or (multiple-of? 5 i)
(multiple-of? 3 i)))
i)
Surely, that's much more readable. In my humble opinion. :-)
-
Notwithstanding the last comment, your formatting for your
primes procedure is not ideal. Here's a more proper formatting:`(define (primes current total)
(if (
Context
StackExchange Code Review Q#78401, answer score: 3
Revisions (0)
No revisions yet.