64
\$\begingroup\$

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

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\$
    – manatwork
    Oct 10, 2016 at 12:20
  • 2
    \$\begingroup\$ Training spaces allowed on each line? \$\endgroup\$
    – Luis Mendo
    Oct 10, 2016 at 12:33
  • 2
    \$\begingroup\$ Is a trailing new line acceptable? \$\endgroup\$
    – Fatalize
    Oct 10, 2016 at 12:34
  • 1
    \$\begingroup\$ Is a leading newline allowed? \$\endgroup\$
    – Riley
    Oct 10, 2016 at 14:38
  • \$\begingroup\$ I don't see a reason why not. \$\endgroup\$
    – Sickboy
    Oct 11, 2016 at 11:35

153 Answers 153

2
\$\begingroup\$

Common Lisp, SBCL, 69 66 61 45 44 41 bytes

full program:

(dotimes(i(read))(format t"~v{*~}*
"i 1))

Explanation

(dotimes(i(read)) <-- loop from i=0 to i=INPUT-1
(format t"~v{*~}*
"i 1)) <-- loop, displaying i+1 asterisks per line

Ideas for improvement are welcomed.

\$\endgroup\$
2
\$\begingroup\$

QBIC, 16 bytes

:#*|[1,a|B=B+A?B

Explanation:

:         Gets a cmd line param as number called 'a'
#*|       Define A$ as '*'
[1,a|     FOR b = 1 to a
B=B+A     Extend B$ with another astrix
?B        Print B$
<FOR LOOP implicitly closed>

QBIC's seen a few updates since this answer was posted. This could now be done with these 12 bytes:

[:|B=B+@*`?B
\$\endgroup\$
2
\$\begingroup\$

Excel VBA, 32 Bytes

Anonymous VBE immediate window function that takes input from range A1 and outputs to the VBE immediate window.

For i=1To[A1]:?String(i,42):Next
\$\endgroup\$
2
\$\begingroup\$

Canvas, 3 bytes

*×]

Try it here!

Explanation:

  *×]  full program
{  ]  map over [1..input]
  *×      repeat by *
\$\endgroup\$
2
\$\begingroup\$

k, 16 12 bytes

4 bytes removed thanks to ngn! :)

{`0:,\x#"*"}

{          } /function(x)
      x#"*"  /reshape "*" by x
    ,\       /scan concatenation through the list
 `0:         /print line by line
\$\endgroup\$
1
  • 1
    \$\begingroup\$ (1+!x)#'"*" -> ,\x#"*" \$\endgroup\$
    – ngn
    Jan 13, 2018 at 22:12
2
\$\begingroup\$

Japt -R, 8 4 bytes

õ_î*

Try it online!

4-byte alternative:

°Tî*

Try it online

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You know, instead of adding a newline to every line, joining with spaces and trimming, you could just join with newlines ;-) \$\endgroup\$ Feb 27, 2017 at 14:56
  • \$\begingroup\$ Could you use this 5 byte version? \$\endgroup\$
    – Shaggy
    Jun 19, 2017 at 12:18
2
\$\begingroup\$

Wren, 41 bytes

Fn.new{|i|(1..i).map{|n|"*"*n}.join("
")}

Try it online!

Explanation

Fn.new{|i|                             // New function with the parameter i
          (1..i)                       // Generate a range from 1 to i
                .map{|n|"*"*n}         // Replace the numbers with asterisks
                              .join("
")}                                    // Join with newline
\$\endgroup\$
2
\$\begingroup\$

MathGolf, 4 bytes

╒⌂*n

Try it online or verify some more test cases.

Explanation:

╒     # Push a list in the range [1, (implicit) input]
 ⌂    # Builtin for the asterisk character "*"
  *   # Map each value to that amount of asterisk characters (similar as in Python)
   n  # And join this list of strings by newlines
      # (after which the entire stack joined together is output implicitly as result)
\$\endgroup\$
2
\$\begingroup\$

SNOBOL4 (CSNOBOL4), 59 52 bytes

	n =input
o	output =o =o '*' lt(size(o),n) :s(o)
end

Try it online!

\$\endgroup\$
2
\$\begingroup\$

C++ (gcc), 61 bytes

int f(int n){for(n&&f(n-1);~n;__builtin_putchar(n--?42:10));}

Uses ASCII char codes and builtins to shorten another solution.

Try it online!

\$\endgroup\$
0
2
\$\begingroup\$

J, 8 bytes

[\@$&'*'

Explanation:

  1. $&'*': Reshape * according to.
  • $: Reshape.
  • &: Bound with (partial application).
  • '*': Character literal *.
  1. @: Function composition.

  2. [\: Each prefix of.

  • [: Identity.
  • \: Apply the preceding verb to each prefix of the right argument.
\$\endgroup\$
1
  • \$\begingroup\$ Looks like someone beat me to it. They used ] instead of [, and # instead of $, but it's conceptually the same thing. \$\endgroup\$
    – Moonchild
    Jul 13, 2020 at 9:00
2
\$\begingroup\$

Vyxal j, 3 bytes

×*¦

Try it Online!

How?

×*¦
×*  # Repeat an asterisk the implicit input amount of times
  ¦ # Prefixes
    # j flag joins on newlines
\$\endgroup\$
2
\$\begingroup\$

Since the course is taught in C++, I'm eager to see solutions in C++.

C++, 123 bytes

(the joke here is to deliver a 'pure' c++ answer)

#include<string>
class X:public std::string{public:X(int i):std::string(i*(i+1)/2,'*'){for(;i--;)insert(i*(i+1)/2,"\n");}};

Test

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

Gema, 36 characters

*=@repeat{*;@append{s;\*}@out{$s\n}}

Sample run:

bash-4.3$ gema '*=@repeat{*;@append{s;\*}@out{$s\n}}' <<< 5
*
**
***
****
*****
\$\endgroup\$
1
\$\begingroup\$

Forth, 39 bytes

Quite simple, with a couple of nested loops. ASCII 42 is an asterisk.

: f 0 do I -1 do 42 emit loop CR loop ;

Try it online

Prints a trailing newline.

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

Bash, 35 characters

for((;i++<$1;));{ s+=\*;echo "$s";}

Sample run:

bash-4.3$ bash draw-an-asterisk-triangle.sh 5
*
**
***
****
*****
\$\endgroup\$
1
\$\begingroup\$

C++, 108 chars

#import<iostream>
main(){int n;std::cin>>n;for(int i=1;i<=n;++i){for(int j=i;j--;)std::cout<<"*";puts("");}}

Straightforward.

Try it online

\$\endgroup\$
4
  • \$\begingroup\$ Can the < be moved directly next to the #include? (I don't do C++, just a wild guess based on Java knowledge) \$\endgroup\$ Oct 10, 2016 at 14:30
  • \$\begingroup\$ You're right.__ \$\endgroup\$
    – John Doe
    Oct 10, 2016 at 14:31
  • 1
    \$\begingroup\$ The #include does need to be included in the byte count. \$\endgroup\$
    – mbomb007
    Oct 10, 2016 at 14:39
  • \$\begingroup\$ Thanks for the C++ solution, but there's a shorter one ;-) \$\endgroup\$
    – Sickboy
    Oct 11, 2016 at 11:33
1
\$\begingroup\$

Vitsy, 19 16 bytes

Yeah this needs some reducing. I'm out of practice!

D1H}\[\['*'O]aO]

(implicit input)
D                 Peek n, push n.
 1                Push 1.
  H               Pop x, pop y, push range(x, y)
   }              Reverse, pop n, reverse, push n.
    \[         ]  Pop n, do bracketed items n times.
      \[    ]     Pop n, do bracketed items n times.
        '*'O      Output the character *.
             aO   Output a newline.

Think of it as though I'm iterating through a list (1 through n) and popping out the number of *s according to the currently selected list item.

Explanation soon.

\$\endgroup\$
3
  • \$\begingroup\$ Is implicit input new for vitsy? \$\endgroup\$ Oct 11, 2016 at 0:30
  • \$\begingroup\$ @ConorO'Brien Nope, that's been around since the beginning for numerical inputs through the command line. \$\endgroup\$ Oct 11, 2016 at 0:31
  • \$\begingroup\$ Huh. My memory fails me, then. \$\endgroup\$ Oct 11, 2016 at 0:32
1
\$\begingroup\$

Clojure, 59 bytes

#(doseq[m(range 1(inc %))](println(apply str(repeat m\*))))

Basically, just uses doseq to loop over the range, printing the corresponding number of stars.

It's really unfortunate that the shortest way I've been able to repeat a character in Clojure is (apply str (repeat m \*)). That's hardly competitive here. Some good ol' python string multiplication would have been awesome.

Ungolfed:

(defn tri [n]
  (doseq [m (range 1 (inc n))]
    (println (apply str (repeat m \*)))))

Should be fairly self-explanatory.

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

Befunge93, 31 chars (3 spaces, 8 pure directionals)

&>:#v_@
-^  : >$55+,1
,"*"<_^#:-1

Befunge98, 28 chars

&>:#v_@
-^  : >$a,1
1,*'<_^#:-
\$\endgroup\$
2
  • \$\begingroup\$ This prints the triangle upside down \$\endgroup\$
    – Jo King
    Dec 29, 2017 at 13:45
  • \$\begingroup\$ The enemy's gate is down \$\endgroup\$ Jan 14, 2018 at 15:45
1
\$\begingroup\$

Ruby, 32 bytes

1.upto($*[0].to_i){|i|puts"*"*i}

The $* is an array of arguments.

foo.rb 5 will print the following:

*
**
***
****
*****
\$\endgroup\$
9
  • \$\begingroup\$ Unless stated otherwise in the question, the answers should be full programs or functions. In both cases input and output should be handled properly either explicitly by the code itself, or implicitly by the interpreter, if it has such feature. You can not assume your code will find input data in some variable nor to just leave the result in a variable. As it is now, your code is considered a snippet, which is not a valid answer. \$\endgroup\$
    – manatwork
    Oct 11, 2016 at 9:42
  • \$\begingroup\$ ARGV[0]? Sure? Ruby 2.3.1 I use receives command line arguments as String, not Fixnum. \$\endgroup\$
    – manatwork
    Oct 11, 2016 at 9:51
  • \$\begingroup\$ is the output of that okay? It's kinda implicit, but it is returned. \$\endgroup\$
    – IMP1
    Oct 11, 2016 at 10:04
  • 1
    \$\begingroup\$ If you ask me, is not valid. Katenkyo suggested in meta to allow such things as he claims Lua programs are able to return string. His suggestion received only 1 upvote and 1 downvote, so there is no consensus on that. In meantime I consider “Programs may output using their exit code... ... if and only if the challenge requires an integer as output, obviously.” relevant with 40 upvotes and 5 downvotes. \$\endgroup\$
    – manatwork
    Oct 11, 2016 at 10:19
  • 1
    \$\begingroup\$ "Returning a string from a program" doesn't seem well-defined since it's in no way observable. You can fix your program at the cost of a single byte by using $><<"*"*i instead of "*"*i+$/ though. \$\endgroup\$ Oct 11, 2016 at 10:23
1
\$\begingroup\$

Excel VBA, 109 bytes

This function returns to cell, requires text wrapping be turned on.

Function a(b)
For i = 1 To b
For c = 1 To i
d = "*" & d
Next c
a = a & d & Chr(10)
d = ""
Next i
End Function

99 Bytes - Alternative that runs as a function but prints to immediate.

Function f(b)
For i = 1 To b
For c = 1 To i
Debug.Print "*";
Next c
Debug.Print
Next i
End Function

94 Bytes - One more that is just a sub with hard coded value, prints to immediate.

Sub k()
r = 5
For i = 1 To r
For c = 1 To i
Debug.Print "*";
Next c
Debug.Print
Next i
End Sub
\$\endgroup\$
1
\$\begingroup\$

PHP, 66 58 bytes

Just a simple loop, really.

I'm new to golfing, so might not be the shortest way.

for($i=fgets(STDIN);$x++<$i;)echo str_repeat("*",$x)."\n";

https://eval.in/658719

Thanks to manatwork for byte-saving.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ No need to explicitly initialize $x, so you can leave that out and move the initialization of $i in its place to spare 1 ; separator. And you can combine the loop's test with the loops stepping: for($i=fgets(STDIN);$x++<$i;)echo str_repeat("*",$x)."␤"; \$\endgroup\$
    – manatwork
    Oct 11, 2016 at 15:24
1
\$\begingroup\$

Javascript, 71 bytes 48 Bytes, thanks ETHproductions!

(n,c)=>{while(c<=n)console.log("*".repeat(c++))}
\$\endgroup\$
1
  • \$\begingroup\$ Welcome to PPCG! You don't need to call the function, just define it: (n,c)=>{while(c<=n){console.log("*".repeat(c++));}} is a valid entry. Also, you don't need the braces in the while loop, nor the semicolon at the end: (n,c)=>{while(c<=n)console.log("*".repeat(c++))} \$\endgroup\$ Oct 11, 2016 at 15:21
1
\$\begingroup\$

Ruby, 29 bytes

->n{n.times{|i|puts'*'*i+?*}}

Call with ->n{n.times{|i|puts'*'*i+?*}}[number].

\$\endgroup\$
1
  • \$\begingroup\$ No need for parenthesis around the proc parameter and is shorter to add a literal character than to add 1 to the multiplier ('*'*(i+1)'*'*i+?*). (BTW, you can call anonymous proc without assigning it to a variable: ->n{n.times{|i|puts'*'*i+?*}}[5].) \$\endgroup\$
    – manatwork
    Oct 11, 2016 at 9:49
1
\$\begingroup\$

VBA, 84 bytes

taking advantage of recursive coding. it generates x times * as the line/round

Function f(x)
If x<=1 Then f="*" Else f=f(x-1) & vbCrLf & String(x,"*")
End Function

results:

?f(10)
*
**
***
****
*****
******
*******
********
*********
**********
?f(2)
*
**

?f(5)
*
**
***
****
*****
\$\endgroup\$
1
\$\begingroup\$

Common Lisp (Lispworks), 71 bytes

(defun f(n)(dotimes(i n)(dotimes(j(1+ i))(format t"*"))(format t"~%")))

Usage:

CL-USER 170 > (f 5)
*
**
***
****
*****
NIL

Ungolfed:

(defun f (n)
  (dotimes (i n)
    (dotimes (j (1+ i))
      (format t "*"))
    (format t "~%")))
\$\endgroup\$
1
\$\begingroup\$

MoonScript, 31 bytes

(n)->for i=1,n
 print "*"\rep i

Sample call:

_ 5
\$\endgroup\$
1
\$\begingroup\$

F#, 114 chars

[<EntryPoint>]
let main a =
 for i = 1 to System.Int32.Parse(a.[0]) do
  printfn "%s" <| String.replicate i "*"
 0
\$\endgroup\$
2
  • \$\begingroup\$ Welcome to the site! I don't know anything about F#, but it looks like you might be able to take away some of the whitespace? \$\endgroup\$
    – DJMcMayhem
    Oct 13, 2016 at 16:18
  • \$\begingroup\$ Thanks! My first golf, both in my company and on the site. I did try and so far it looks like the other "type" of F# syntax is "verbose", which just gets longer. The indentation is the "light" syntax, which at minimum is a new line char and a space to indent. We do these in our slack channel but i didn't see any F# answers here, so i figured I would post! I can update with test cases but anyone can copy into a new F# console app and run pretty easily with VS 2015 Community and the F# tools, all of which are free. \$\endgroup\$ Oct 13, 2016 at 23:49
1
\$\begingroup\$

Braingasm, 27 bytes

Braingasm is a brainfuck variant with just a few more instructions and options. And this task would've been so much easier if I had bothered implementing that as many times as the value in the current cell thing yet..

;[->+[->+42.<]10.>[-<+>]<<]

Here's how it works:

;                           Read an integer from stdin and write it to cell#1
 [                        ] While the value of cell#1 is not zero,
  ->+                    <    substract one from cell#1 and add one to cell#2.
     [->+   <]                Each time, do the same between cell#2 and cell#3,
         42.                    but also print an asterix on the way.
              10.             Then print a newline.
                 >[-<+>]<     Move the value from cell#3 back to cell#2
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.