Implement an algorithm which increases number and returns the result. Start from number and increment by step as many times as iterations demands.
Input: number, iterations, step.
Example:
alg(2, 5, 10) => 2 + 10 + 10 + 10 + 10 + 10 = 52
alg(17, 3, 6) => 17 + 6 + 6 + 6 = 35
Tests:
alg(100, 5, 50)
alg(14, 20, 4)
Good luck!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Oldest comments (17)
In JS:
smart
Thought I'd give this a try using plain lambda calculus. The notation is:
λx.t= Function taking x as an argument and producing the expression t. Can simplifyλx.(λy.t)->λx y.tMN= Apply N to MMNO=(MN)OExample 1:
here's the solution in python :

in ruby
Clojure - Level 1 🌟
Go
Clojure - Level 2 🌟🌟
Compared to your "level 1", this is more like the question asked, since it asks for an algorithm rather than the answer. (I totally agree that it should be
(+ number (* step iterations))).In Clojure it's usually better to preference core functions, followed by
reduce, followed byloop/recur.The clojure.core function approach is just:
But that's really using
iterateto do the algorithm part. Soreducemight be more in the spirit of things. It's shorter too 🙂Awesome breakdown! Thanks Paula. 🙏🏿
I feel like you're right on the hierarchy of abstraction to use for Clojure functions (there's just so many, and many of them are composed)._
I could've just used pure-recursion without
loop/recurbut I don't get the power of the AST's tail-end optimizer for loops. Thank you for reminding me of the thread-macro.C
python answer:
def alg(add, mult, num):
return add + mult*num
JS answer:
alg = (num1, num2, num3) => num1 + num2 *num3;
In Python:
Another 'short'-cut:
Applying function