import java.util.Scanner;
public class DailyProgrammer {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Number of Lines: ");
int numLines = Integer.parseInt(in.nextLine());
StringBuilder sb = new StringBuilder("@");
for (int i = 0; i < numLines; i++) {
System.out.println(sb.toString());
sb.append(sb.toString());
}
}
}
the expression (x << y) means "Shift the binary representation of the integer x left by y places, then fill the space to the right with 0s." In arithmetic terms, this very efficiently implements the operation (x * 2y ). For example, 13 << 4 would be 0b1101 with four zeros on the right, so 0b11010000, which is 1324 = 1316=208.
Since I have 1 << n, then I am doing 1*2n, so the integer the expression evaluates to is 2n. Therefore, s is a string of "@" symbols of length 2n.
Since this is python and not C, I probably could have gotten away with 2**int(raw_input()) instead, but old habits die hard I guess.
Common Lisp with 1 bonus: (Disclaimer, I'm just starting)
(defun print_triangle(height dir &optional (m height))
(if (not (zerop height))
(progn
(if (zerop dir)
(loop for i from 1 to (expt 2 (- height 1)) do (format t "@"))
(loop for i from 1 to (expt 2 (abs(- height m))) do (format t "@")))
(format t "~%")
(print_triangle(- height 1) dir m))))
.if (\n[right] == 1) \{\
.ad r
.\}
.nr a 0
.nr b 1
.while (\na < \n[height]) \{\
.nr cpt 0 1
.while (\n+[cpt] < \nb) \{\
@
.\}
@
.nr b \nb*2
.nr a +1
.\}
Usage: groff -Tascii -rheight=5 triangle.roff to display a triangle of height 5. Can also print the triangle right justified, with the option -rright=1
def triangle(height):
line = '@'
x= 1
if height == 0:
print('Triangle of height 0 not valid!')
while x <= height:
print(line)
line *= 2
x+=1
def reversetriangle(height):
line = '@'
x= height
if height == 0:
print('Triangle of height 0 not valid!')
while x > 0:
line = '@'*2**(x-1)
print('{:>70}'.format(line))
x-=1
6
u/Haruhi_Suzumiya Mar 04 '12
http://ren.pastebay.org/310243
2
u/brbpizzatime Mar 04 '12
C++: http://pastebin.com/mJX2UzqH
2
u/[deleted] Mar 04 '12 edited Mar 04 '12
Perl:
Reverse Extra Credit:
1
u/I_AM_A_BICYCLE Mar 04 '12 edited Mar 04 '12
Java
1
u/turboemu Mar 04 '12
Java with reverse: http://pastebin.com/9UKrgyjf
1
u/Steve132 0 1 Mar 04 '12 edited Mar 04 '12
Python:
1
u/drb226 0 0 Mar 04 '12
In Haskell:
Usage:
1
u/Devanon Mar 05 '12
In Ruby:
1
u/Cosmologicon 2 3 Mar 05 '12
shell script
1
u/[deleted] Mar 05 '12
Javascript. No methods I am aware of to create multiple characters at once.
http://pastebin.com/RVraLr6S
1
u/cooper6581 Mar 05 '12
Common Lisp with 1 bonus: (Disclaimer, I'm just starting)
Output:
1
u/geraudster Mar 05 '12
With Groff (http://www.gnu.org/software/groff/) :
Usage: groff -Tascii -rheight=5 triangle.roff to display a triangle of height 5. Can also print the triangle right justified, with the option -rright=1
1
u/school_throwaway Mar 05 '12
Python 2.7 with bonus
1
u/jnaranjo Mar 06 '12
A quickie python solution
1
u/CeilingSpy Mar 07 '12
Prints the triangle in the middle:
1
u/lil_nate_dogg Mar 08 '12
1
u/patrickgh3 Mar 10 '12
My solution in java, with both extra credits: http://pastebin.com/mJmhnbXB
1
u/Yuushi Mar 13 '12
Scheme, with all:
1
u/ladaghini Mar 24 '12
Python:
1
u/emcoffey3 0 0 May 05 '12
C#
1
u/Should_I_say_this Jun 24 '12
python 3.2 with extra credit