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
Printable View
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 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 :p
Look at it this way and it becomes obvious.
Code:1
2 4
3 6 9
4 8 12 16
...
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
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.Quote:
care to share a hint about how you solved this with two lines of code?
PM'ed code to Elkvis and Matticus
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
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.
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.
You make a fair point.
Code:print('\n'.join(str(tier) for tier in [' '.join(str(i*j) for i in range(1, j+1)) for j in range(1, 11)]))
I see your point, but then using one for loop was about the only thing that was clarified, so I think most of us assumed it was mandatory. My alternative suggestion, before everyone started talking about their solutions for printing that string was to post something like this:
This might, in fact, be a more correct solution to the OP's question if they meant quite literally:Code:#include <stdio.h>
int main() {
int i;
for (i=0; !i; i++)
return 0;
}
Any use of one of those numbers might be considered inaccurate. In fact, I chose to be even more pedantic by using !i as every positive digit from 1-9 is present by itself in the OP's sequence which one could argue disallows any number that contains them. Though, of course, there are other ways of do a for loop without using any of those numbers, as well. I wasn't actually sure if a blank for(;;) was legal by the C standard or if it was just the compiler I was using, but I probably would have preferred that if it was.Quote:
CAN ANYONE HELP ME to write a c program using only "one for loop" and without using a 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
Ah, in that case, I would propose this two line C99 program:
Code:int main(void) {
for (; 0;); }
Yeah that's about as compact as it gets. I should really feel silly for leaving in my compiler's templated #include <stdio.h> in a program that wasn't calling any library functions.
I accept that you abandoned the "using C" requirement, but I can't forgive you for technically using more than one for.
Eh. I guess it could be fixed. I suspect people would get bored of me though.