## Two Easy Tasks

### March 3, 2020

It doesn’t take any programming to solve the first problem: since 13 and 17 have no common factors, the possible solutions are 13 × 17 = 221, and any multiples of 221 that are less than 500. But since we have to write a program:

> (filter (lambda (n) (and (zero? (modulo n 13)) (zero? (modulo n 17)))) (range 500)) (0 221 442)

I don’t know if 0 is included in the problem domain, so the correct answer to Jason’s question is either 2 or 3. The solution sheet is wrong. I’m not sure which is worse, the question or the answer.

The second problem is easily solved using Gauss’ formula for the sum of the first *n* integers, distributing the modulo throughout the calculation:

(define (f n) (let* ((m (+ #e1e9 7)) (n (modulo n m))) (modulo (* n (+ n 1) 1/2) m))) > (f 1234567) 78450694

You can run the program at https://ideone.com/zdX3kr.

Yet another reason why multiple choice problems don’t make sense in a programming setting! Anyway, I got 2 or 3 too, depending on what the starting point is: 0, 221, and 442. Not sure what the creator of the problem was smoking, in order to come up with 4 as the correct answer, but I’m pretty certain it wasn’t over-the-counter stuff.

Cheers

yeah i think like zack his wrong. 13 and 17 are prime… so their first common multipler is 221… and the 2nd 442. it’s impossible have 4 as result. cya.

Note: since the modulus is odd, we must divide by 2 before taking the final modulo, because even[odd] can give odd. But n[odd]*(n+1)[odd] will be even.

Here are two Python solutions to the first problem.

Output:

Here’s a C solution to the second problem.

Example:

Here’s a Haskell version. Instead of relying upon 64-bit numbers I use typesafe modular numbers from the arithmoi library.

Klong version