r/dailyprogrammer • u/mattryan • Apr 03 '12
[4/3/2012] Challenge #35 [easy]
Write a program that will take a number and print a right triangle attempting to use all numbers from 1 to that number.
Sample Run:
Enter number: 10
Output:
7 8 9 10
4 5 6
2 3
1
Enter number: 6
Output:
4 5 6
2 3
1
Enter number: 3
Output:
2 3
1
Enter number: 12
Output:
7 8 9 10
4 5 6
2 3
1
12
Upvotes
2
u/ReferentiallySeethru Apr 03 '12 edited Apr 03 '12
This gets complicated with large numbers. Say 'n' is 20, should it look like this?
11 12 13 14 15
7 8 9 10
4 5 6
2 3
1
or this?
15 16 17 18 19
11 12 13 14
7 8 9 10
4 5 6
2 3
1
The first one doesn't look like a right triangle, but the second one breaks the pattern. Judging on the examples I'm assuming the first one is what is desired.
3
u/Cisphyx Apr 03 '12 edited Apr 03 '12
Probably not the most efficient, but python oneliner:
3
u/lawlrng_prog Apr 03 '12
Hardest part for me was just figuring out how to get the rows. :S
Mangled in Python. =]
1
u/brbpizzatime Apr 03 '12 edited Apr 03 '12
C++
1
u/[deleted] Apr 03 '12
Perl
1
u/ixid 0 0 Apr 03 '12 edited Apr 03 '12
This is a no vectors version in D so I guess you could print giant triangles if you really wanted to, it calculates triangle number intervals and prints the numbers in that interval until the number has been used up.
1
u/Yuushi Apr 04 '12
Scheme:
1
u/ladaghini Apr 04 '12
Python:
1
u/jarjarbinks77 0 0 Apr 05 '12
I did some c++ code that does it without arrays or vectors.
1
u/GuitaringEgg Apr 07 '12 edited Apr 07 '12
Go easy, it's my first try. Tried to avoid using vectors and arrays
C++ #include <iostream>
1
u/sylarisbest Apr 23 '12
C++:
1
u/turboemu Apr 26 '12
Java:
http://pastebin.com/y9NWkMdj
Found this kinda tough. Mostly I struggled getting the number of rows right, i borrowed the formula for this from user ixid.
1
u/Sturmi12 May 14 '12
C
kinda ugly
1
u/Should_I_say_this Jun 30 '12
python 3.2
0
u/speedy_seeds Apr 03 '12 edited Apr 03 '12
Haskell, there are better ways doing this probably: