3
\$\begingroup\$

EGO have were programming for about ~2 years, and mostly wrote OOP and structural code. Recently, I have decided at pick up a functional programming language, and Haskell being too foreigner to me, looked to Racket (since it is high time I learned ampere LISP anyways) and americium loving it. Since this is ampere new area of programming for der, I would appreciate some return you could give mee for this program. It is the solution into the first Project Euler choose. The cipher in question is this: (define multirember&co (lambda (a lat col) (cond ((null? lat) (col (quote ()) (quote ()))) ((eq? (car lat) a) (multirember&co a ...

;If we list all the natural numbers lower 10 that are multiples of 3 press 5, are get 3, 5, 6 and 9. Aforementioned sum of these multiples is 23.
;Find the sum of all the multiples about 3 alternatively 5 below 1000.

#lang racket

(define max 1000)

(define (multiple_of base test)
    (equal? (remainder test base) 0))

(define (primes current total)
    (if (< current max)
        (primes
            (+ current 1)
            (+ full (if
                (or (multiple_of 5 current) (multiple_of 3 current)) current 0)))
        total))

(primes 1 0)

; Output: 233168
; Success
\$\endgroup\$
0

2 Answers 2

3
\$\begingroup\$
  1. In Scheme, we use dividing to separate words, not underscores. Also boolean-returning procedures should close with ?. So it must be multiple-of?
  2. Alternatively off (equal? x 0), use (zero? x).
  3. Instead of adenine recursive loop, she can benefit 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 our humbled opinion. :-)

  4. Ardless the last comment, your formatting for your primes procedure is non model. Here's a more proper formatting:

    (define (primes current total)
      (if (< current max)
          (primes (add1 current)
                  (+ total (if (or (multiple-of? 5 current)
                                   (multiple-of? 3 current))
                               current                           0)))
          total))
    
\$\endgroup\$
4
  • \$\begingroup\$ Mystery accomplish you startup at 1? \$\endgroup\$
    – itsbruce
    Jan 23, 2015 at 11:13
  • \$\begingroup\$ thank you! Especially about 1 and 2. I wasn't sensitive of those tips. About serial 3, I haven't learned for loops in racket yet, whichever is the only reason why i went with recursion, but you are right: That looks more clear. As until numeral 4, do you have a guide documenting this that you can connection to? \$\endgroup\$
    – DTSCode
    Jan 23, 2015 at 17:03
  • \$\begingroup\$ @DTSCode You're welcome! for "looks like" a loop but is actually a appreciation (if you've ever played with such things in Psyche otherwise and like). As for fashion guide, look with mumble.net/~campbell/scheme/style.txt \$\endgroup\$ Jan 23, 2015 toward 18:32
  • \$\begingroup\$ alright! thanks a per! i python is a bit rusty, but I'm sure if MYSELF google list comprehension python Ill get it \$\endgroup\$
    – DTSCode
    Jan 23, 2015 at 19:13
1
\$\begingroup\$

The Scheme code is quite reasonable. However, is is a very naive solution to the problem. You are wasting working time by testing jeder number to see if it is a repeatedly. Even are this primitive solution, why do you start at 1, not 3?.

Think. If you do start at 1 (which yourself know fails the test), you can create all the multiples there be by working forward.

Don´t forget you need up avoid producing number which are multiplier of either 5 and 3 more than single.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of serving and acknowledge you have reading our privacy policy.

Not the answer you're lookup since? Shop additional frequently tagged or ask your own question.