r/dailyprogrammer_ideas • • May 14 '17
[Easy] print numbers 1 through 100 without using loops.
Easy interview question.
5
Upvotes
r/dailyprogrammer_ideas • • May 14 '17
Easy interview question.
3
u/[deleted] May 17 '17
By the way, using 100 print statements would be a valid answer here too :)
7
u/MasterAgent47 May 14 '17
Without loops....
or any recursion.
....
3
u/[deleted] May 14 '17 edited May 14 '17
c
3
u/jnazario May 14 '17
a solution in F#
as a mod i have to admit i'm of two minds about this one. i dislike challenges that stipulate how a solution must be found. on the other hand encouraging people to think about recursion is tempting.
3
u/ChazR May 17 '17 edited May 17 '17
In Haskell:
In python:
Each of these does, of course, use a loop. The only way to do this without a loop is like this:
BASIC:
A loop is what happens when a CPU moves the instruction pointer to a point in the code that has already been executed. You can do this with a JUMP or a BRANCH instruction (or with a few other things on CPUs with hardware stacks). Explicit loops in high-level languages, recursion, using ranges - all these compile down to jumps and branches.
This idea might cause some of us old greybeards to break out INTERCAL for fun, but it's a bit arbitrary, and either not very interesting, or very, very difficult.
1
u/Mattgame555 May 14 '17
Not really sure if this is suitable for this sub. Many languages have out of the box support for this kind of operation such as Java, python and C# using LINQ meaning it isn't much of a challenge. Languages without these features pretty much can't complete this challenge assuming the use of common structures such as a while loop with an counter variable is forbidden - which I'm assuming it is.
2
u/chunes May 17 '17 edited May 17 '17
So how about in a functional language where functions like map and each are defined recursively?
1
u/JakDrako May 16 '17
vb.net using recursion
1
u/Philboyd_Studge May 14 '17
java 8