Two Simple Tasks
January 5, 2021
Happy New Year! May 2021 be a better year than 2020.
We have two simple tasks today:
First: Write a program that prints the sequence 1, 5, 10, 50, 100, … up to a given limit.
Second: Write a program that finds all three-digit numbers n such that n/11 equals the sum of the squares of the three digits.
Your task is to write programs that solve the two tasks given above. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
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