Two Simple Tasks
January 5, 2021
The first task is a simple loop:
(define (f limit)
(let loop ((n 1) (m 5))
(when (<= n limit)
(display n) (newline)
(loop (* n m) (/ 10 m)))))
> (f 10000) 1 5 10 50 100 500 1000 5000 10000
The second task is easy enough to work by hand, since there are only 81 possibilities from 110 to 990. Or you can do the algebra. But we solve the problem with a list comprehension:
> (list-of n (n range 110 1000 11) (= (sum (map square (digits n))) (/ n 11))) (550 803)
You can run the program at https://ideone.com/eUnbyO.
I felt like generalizing the first problem a bit and am including my
solution in standard R7RS Scheme below, along with a simple solution
to the second one as well. Happy 2021 to all!
Such a good way to start the new coding year :-) Here is my take on this drill using Julia 1.5.2: https://pastebin.com/AjpJg1Hn. Cheers
Here’s a solution to the first task in x86-64 assembly using Linux system calls.
Example usage:
Here’s a solution to the second task in x86-64 assembly using Linux system calls.
Example usage:
A Lua solution for the first task.
Lua solution for the first task using mutual tail calls.
Lua solution for the second task. Brute force stepping through all the numbers from 100 to 999 rather than just multiples of 11. This allows interpreting n/11 as floor division (see commented line), giving more solutions.
A response for “Two Simple Tasks”, written in Racket:
And here are the results:
I’m trying to learn JavaScript. So here are my answers written in JS and run with node…
var start = [“1”, “50”]
function addzero(t) {
return [t[0] += ‘0’, t[1] += ‘0’]
}
let max = 1000000
for (var i = 0; i < 10; i++) {
var r = addzero(start)
if (r[0] > max || r[1] > max) return
console.log(r.shift(),r.shift())
}
// prax1.js
for (let i = 100; i < 1000; i++) {
let d = i.toString().split(”)
let dig = d.map(Number)
let sum = dig[0] * dig[0] + dig[1]* dig[1] + dig[2] * dig[2]
if (i/11 == sum) console.log(i/11, dig, sum)
}
% node prax1.js
50 [ 5, 5, 0 ] 50
73 [ 8, 0, 3 ] 73