1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 6 12 18 24 30 36 7 14 21 28 35 42 49 8 16 24 32 40 48 56 64 9 18 27 36 45 54 63 72 81 10 20 30 40 50 60 70 80 90 100
1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 6 12 18 24 30 36 7 14 21 28 35 42 49 8 16 24 32 40 48 56 64 9 18 27 36 45 54 63 72 81 10 20 30 40 50 60 70 80 90 100
I'm declaring this the worst question of the month, and it's only the first day!
"...and without using a..."
You put the entire question in the title, but it lost key information at the end.
Using a what?
- Power Drill
- Sheet folder
- copy of TurboC
> 1 2 4 3 6 9 4 8 12
I'm guessing there is some formatting going on here, but who the hell would know from your hasty puke on the forum.
Here, read this.
How To Ask Questions The Smart Way
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
If I had to take a shot in the dark, I'd say they were going for "without using a goto".
EDIT: A like for anyone who decipher those numbers
Last edited by Yarin; 09-01-2013 at 12:57 PM.
Look at it this way and it becomes obvious.
Code:1 2 4 3 6 9 4 8 12 16 ...
The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss
My guess is this.
My answer is 2 lines of code inside a single for loop.Code:1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 6 12 18 24 30 36 7 14 21 28 35 42 49 8 16 24 32 40 48 56 64 9 18 27 36 45 54 63 72 81 10 20 30 40 50 60 70 80 90 100
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I'm just not as smart as you guys.
Code:#!/usr/bin/perl #1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 6 12 18 24 30 36 7 14 21 28 35 42 49 8 #16 24 32 40 48 56 64 9 18 27 36 45 54 63 72 81 10 20 30 40 50 60 70 80 90 #100 #argh. I can't see a pattern...oh well. for my $i (1 .. 55){ if($i == 3){ print '4'; }elsif($i == 4){ print '3'; }elsif($i == 5){ print '6'; }elsif($i == 6){ print '9'; }else{ #etc... print $i; } }
I have no idea how Salem did it, but I was able to make it happen using some comma-operator trickery.care to share a hint about how you solved this with two lines of code?
PM'ed code to Elkvis and Matticus
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Well, if you use the ternary operator, you can easily do it with a single instruction (in the body of the loop).
The body of my loop:
Output:Code:fprintf(stdout,"%d ", k);
* HINT: Don't forget you can do as many assignments as you need in the loop instruction.Code:1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 6 12 18 24 30 36 7 14 21 28 35 42 49 8 16 24 32 40 48 56 64 9 18 27 36 45 54 63 72 81 10 20 30 40 50 60 70 80 90 100
Sent from my iPadŽ
I did not even think of the ternary operator before when I was trying to figure this out. I used ... other methods, to achieve the two-line result.
Also, my solution incorporated newlines per post #5.
Effectively you can consider commas and the ternary operator to be cheating as they would both be evaluating multiple instructions in one statement. Though, admittedly, I can't see the math I think I need to eliminate the condition. With the way I'm using the ternary operator putting the newline characters is trivial as the loop maintains both the base term of each line and a sequential figure. Suffice it to say there is more than one way to cook this egg. Mine might just have a bit too much salt.
Last edited by SlyMaelstrom; 09-03-2013 at 04:48 PM.
I was about to figure out what Salem did, but then I stopped caring and tried to do it even shorter in python.
Code:for k in [[j*i for i in range(1, j+1)] for j in range(1, 11)]: print(k)
It looks like there has still been no clarification. In the absence of clarification, I'd ask: why on earth would anyone need a loop to print a single string once? Unless you want to format the string over multiple lines, a correct solution here is a one-liner, not a two-liner, and a loop is unnecessary complication.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)