65
\$\begingroup\$

Inspired by a task for Programming 101, here's a challenge that hopefully isn't too easy.

Input:

  • A positive integer n >= 1.

Output:

  • n lines of asterisks, where every new line has one asterisk more than the line before, and starting with one asterisk in the first line.

Test case (n=5):

* ** *** **** ***** 

This is , so shortest answer in bytes wins.

\$\endgroup\$
10
  • 7
    \$\begingroup\$ Not duplicate, just subset of Generate a right triangle. \$\endgroup\$ Commented Oct 10, 2016 at 12:20
  • 2
    \$\begingroup\$ Training spaces allowed on each line? \$\endgroup\$ Commented Oct 10, 2016 at 12:33
  • 2
    \$\begingroup\$ Is a trailing new line acceptable? \$\endgroup\$ Commented Oct 10, 2016 at 12:34
  • 1
    \$\begingroup\$ Is a leading newline allowed? \$\endgroup\$ Commented Oct 10, 2016 at 14:38
  • \$\begingroup\$ I don't see a reason why not. \$\endgroup\$ Commented Oct 11, 2016 at 11:35

158 Answers 158

1 2 3 4 5
6
1
\$\begingroup\$

Erlang (escript), 50 bytes

s(0)->"";s(N)->s(N-1)++string:copies("*",N)++"~n". 

Try it online!

Explanation

s(0)->""; % If the current item is 0, return the empty string. s(N)-> % Otherwise: s(N-1) % Return the N-1 string. ++string:copies("*",N). % Append "*" N times. ++"~n" % Append a newline. 
\$\endgroup\$
1
\$\begingroup\$

Powershell, 24 bytes

1.."$args"|%{$a+="*";$a} 
\$\endgroup\$
1
  • \$\begingroup\$ try this Try it online!. watch out for this Try it online! \$\endgroup\$ Commented Mar 8, 2020 at 9:10
1
\$\begingroup\$

C++ (gcc), 65 bytes

#include<ios> int f(int n){for(n&&f(n-1);~n;putchar(n--?42:10));} 

Including the #include in the byte count.

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ You can shave off 8 bytes by just making it f(n){; int is implied as both parameter and return type. \$\endgroup\$ Commented Jul 13, 2020 at 8:37
1
\$\begingroup\$

Add++, 11 bytes

L,R"*"€*BFn 

Try it online!

A function that returns the string given an argument \$n\$.

A full program is 17 bytes:

y:? Ey,x:"*",*i,O 

Try it online!

How they work

L,		; Define "anonymous" function 		; It takes one argument, e.g	5 	R	; Range			STACK = [[1 2 3 4 5]] 	"*"	; Push "*"		STACK = [[1 2 3 4 5] "*"] 	€	; For each i: 	 *	; Repeat "*" i times	STACK = [["*" "**" "***" "****" "*****"]] 	BF	; Flatten		STACK = ["*" "**" "***" "****" "*****"] 	n	; Join by newlines	STACK = ['*\n**\n***\n****\n*****'] 		; Return the top value 
y:?		; y = input() Ey,		; for i in range(y): 	x:"*",	; x = "*" 	*i,	; x *= i 	O	; print(x) 
\$\endgroup\$
1
\$\begingroup\$

Thunno d, \$9\log_{256}(96)\approx\$ 7.41 bytes

R{1+'**ZK 

Attempt This Online! The slight misallignment on the third line of output is just ATO's formatting.

Explanation:

R{1+'**ZK # Implicit input R{ # Loop through range(0, input) 1+ # Add one * # Multiply this '* # With the string "*" ZK # Print # The d flag stops the implicit print at the end 
\$\endgroup\$
1
\$\begingroup\$

Tcl, 27 bytes

time {puts [append s *]} $n 

Try it online!


# [Tcl], 39 bytes
time {puts [string repe * [incr i]]} $n 

Try it online!


# [Tcl], 46 bytes
while {[incr i]<=$n} {puts [string repe * $i]} 

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Raku (Perl 6) (rakudo), 22 bytes

{say '*'x$_ for 1..$_} 

Attempt This Online!

\$\endgroup\$
1
\$\begingroup\$

Swift 6, 52 bytes

let s={(1...$0).map{print({String.init}()("*",$0))}} 
\$\endgroup\$
1 2 3 4 5
6

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.