r/dailyprogrammer • u/rya11111 3 1 • Apr 16 '12
[4/16/2012] Challenge #40 [easy]
Print the numbers from 1 to 1000 without using any loop or conditional statements.
Don’t just write the printf() or cout statement 1000 times.
Be creative and try to find the most efficient way!
- source: stackexchange.com
13
Upvotes
11
u/JerMenKoO 0 0 Apr 16 '12
http://stackoverflow.com/questions/4568645/printing-1-to-1000-without-loop-or-conditionals
Copied, or just coincidence? The text matches almost perfectly.
12
u/[deleted] Apr 16 '12
C code:
Or in Python:
8
u/Cosmologicon 2 3 Apr 16 '12
C:
5
u/Zamarok Apr 16 '12
Haskell:
5
u/[deleted] Apr 16 '12 edited Apr 16 '12
I threw this together in Clojure in 10 minutes:
The assignment of 1/x to a variable causes a fatal error at 0 hence bringing the program out of the recursion cycle.
12
u/jjduhamer 0 0 Apr 16 '12
c:
:)
3
u/heseov Apr 16 '12
LINQ
2
u/_redka 0 0 Apr 16 '12
2
u/[deleted] Apr 16 '12
A bit lengthy, java n00b-ish solution. Also not sure if throwing an error counts.
2
u/ameoba Apr 18 '12
Python.
Most obvious solution :
print "\n".join(map(str, range(1000+1)))
Some might argue that range() is an implicit loop. You could go with a simple recursive function like this :
...but that doesn't quite work, since the default recursion limit in Python is 1000. An easy hack would be to call f() twice with two different ranges. Only slightly more complex would be to create another function that did the splitting for you but that's not really solving a 'new' problem.
2
u/emcoffey3 0 0 Apr 28 '12
The simplest I can think of is in PowerShell:
Or in C# with LINQ:
3
u/Joe_Pineapples Apr 16 '12 edited Apr 16 '12
Does this even count?:
Bash
As I'm learning ruby atm as well: ruby
4
u/school_throwaway Apr 16 '12
Finally managed a python one liner
4
u/orgelmorgel 0 0 Apr 16 '12
Python
1
u/Koldof 0 0 Apr 16 '12 edited Apr 17 '12
I didn't see this solution here. It didn't come to me at first, but I saw this solution on the SO thread and decided to learn how it worked. After I figured it out, I re-wrote the code and added some documentation.
C++
edit: derp, I quoted by accident.
Output: http://codepad.org/1f88gZl6 Codepad doesn't have a line buffer, so it is way too long. Running it on the Windows command line makes it look lovely.
1
u/drb226 0 0 Apr 17 '12
Super-overly-complicated Haskell version!
1
u/phatcabbage Apr 17 '12
C++ with no conditionals or loops (at least, not when it's running :) )
edit: Fixed my overzealous html entity encoding.
1
u/[deleted] Apr 17 '12
common lisp:
Super simple version,
Still simple, but slightly more versatile version,
1
u/HazzyPls 0 0 Apr 17 '12
No loops, no macro trickery.
Saw this in the StackOverflow version. I love the simplicity, but it isn't really standard or anything. Clang won't even compile
void main. :(1
u/namekuseijin Apr 17 '12
this was fun. in scheme:
a recursive call inside a short-circuit, like many other solutions.
I'm actually more amused that calls to imperative functions such as display and newline actually do provide a value, and one that is considered true! I would expect them to evaluate to #f of all values... perhaps they had this in mind? :p
1
u/Skooljester 0 0 Apr 30 '12
Would this work for Ruby?
1
u/EvanHahn Aug 18 '12
CoffeeScript:
Ruby:
1
u/luxgladius 0 0 Apr 16 '12
Perl
1
u/jaspeh Apr 26 '12
C#
0
u/tehstone Apr 16 '12
I did it in Python using a recursive call but I still have an if else statement so I guess it's not entirely correct. No for or while loops though.
0
u/[deleted] Apr 16 '12
Pascal. Not sure if this counts as a loop
0
u/Hoppipzzz Apr 17 '12
C++ using recursion:
0
u/tuxedotomson Apr 18 '12
C++, I think this is correct.
0
u/[deleted] Apr 16 '12
[deleted] — view deleted comment