Given an integer n, output the following ASCII art to n rows:
1+たす1=わ2
1+たす2=わ3
2+たす3=わ5
3+たす5=わ8
5+たす8=わ13
Essentially, the first row is 1+1=2
and the nth row (1-indexed) is \$f_n + f_{n+1} = f_{n+2}\$ where \$f\$ is the Fibonacci sequence, padded so the numbers line up with the previous row.
You may instead output it infinitely. You may output a list of lines.
This is code-golf, shortest wins!
Testcase
The output for 20 should be:
1+たす1=わ2
1+たす2=わ3
2+たす3=わ5
3+たす5=わ8
5+たす8=わ13
8+たす13=わ21
13+たす21=わ34
21+たす34=わ55
34+たす55=わ89
55+たす89=わ144
89+たす144=わ233
144+たす233=わ377
233+たす377=わ610
377+たす610=わ987
610+たす987=わ1597
987+たす1597=わ2584
1597+たす2584=わ4181
2584+たす4181=わ6765
4181+たす6765=わ10946
6765+たす10946=わ17711
-
3\$\begingroup\$ How exactly is this a triangle? \$\endgroup\$loopy walt– loopy walt2022年03月05日 04:07:48 +00:00Commented Mar 5, 2022 at 4:07
-
5\$\begingroup\$ Fibonacci swoosh? \$\endgroup\$Greg Martin– Greg Martin2022年03月05日 06:10:40 +00:00Commented Mar 5, 2022 at 6:10
-
1\$\begingroup\$ Is leading whitespace allowed? \$\endgroup\$pxeger– pxeger2022年03月05日 17:12:08 +00:00Commented Mar 5, 2022 at 17:12
-
1\$\begingroup\$ @pxeger Allowed? Seems to be required as I read it... \$\endgroup\$Darrel Hoffman– Darrel Hoffman2022年03月07日 16:39:50 +00:00Commented Mar 7, 2022 at 16:39
-
1\$\begingroup\$ @DarrelHoffman I mean constant extra leading whitespace, like this: gist.github.com/pxeger/768a57edc59ce03a673d1d6a943ff0d7 \$\endgroup\$pxeger– pxeger2022年03月07日 18:12:19 +00:00Commented Mar 7, 2022 at 18:12
23 Answers 23
Python 2, 67 bytes
Outputs the sequence indefinitely.
a=b=l=1
while 1:print'%*d+%d='%(l,a,b)+`a+b`;l-=~len(`b`);a,b=b,a+b
JavaScript (ES8), 79 bytes
f=(n,A=B=1,p)=>n?''.padEnd(p)+A+`+${B}=${B+=A}
`+f(n-1,B-A,(A+"").length-~p):''
Commented
f = ( // f is a recursive function taking:
n, // n = input
A = B = 1, // A, B = Fibonacci variables
p // p = padding length, initially undefined
) => //
n ? // if n is not equal to 0:
''.padEnd(p) + // append p spaces (none if p is undefined)
A + // followed by A
`+${B}=${B += A}\n` + // followed by "+[B]=[B+A]\n" (A is added to B)
f( // followed by the result of a recursive call:
n - 1, // decrement n
B - A, // update A to the previous value of B
(A + "") // add the length of A coerced to a string + 1
.length - ~p // to p
) // end of recursive call
: // else:
'' // stop the recursion
C (gcc), (削除) 71 (削除ここまで) 66 bytes
-5 thanks to Sisyphus
i,j;main(k){for(;;)printf("%*d+%d%n=%d\n",i,j=k-j,k+=j,&i,j+k+k);}
Outputs indefinitely.
-
\$\begingroup\$ 66 bytes using
%n
: Try it online! \$\endgroup\$Sisyphus– Sisyphus2022年03月08日 11:47:34 +00:00Commented Mar 8, 2022 at 11:47
Charcoal, 25 bytes
≔E2¦1ηFN«I⌊η+≔⟦⌈ηΣη⟧η⟦⪫η=
Try it online! Link is to verbose version of code. Explanation:
≔E2¦1η
Start with two 1
s.
FN«
Repeat n
times.
I⌊η
Output the first element of the old pair.
+
Output a +
.
≔⟦⌈ηΣη⟧η
Replace the pair with the second element and their sum.
⟦⪫η=
Output the new pair joined with =
and move the cursor down.
Pip, 30 bytes
W P[xi::o'+o+:i'=i+o]x.:sX#i+1
Outputs forever. Attempt This Online!
Explanation
W P[xi::o'+o+:i'=i+o]x.:sX#i+1
i is 0, o is 1, x is "", s is " " (implicit)
[ ] Put the following in a list:
x The indent: x
i::o Swap i (the smaller number) with o (the larger number)
and return the new value of i
'+ Plus sign
o+:i Add i to o in-place and return the new value of o
'= Equals sign
i+o Add i and o
P Print the list (concatenating its elements by default)
W Loop while the list is truthy (which is always):
#i Length of i
+1 Plus 1
sX That many spaces
x.: Concatenate with x in-place
Python 3.8 (pre-release), 66 bytes @emanresu A
a=b=l=1
while l:=len(x:=f"{a:>{l}}+{b}=%d")-3:a,b=b,a+b;print(x%b)
Old Python 3.8 (pre-release), 67 bytes
a=b=l=1
while l:=len(x:=f"{a:>{l}}+{b}"):a,b=b,a+b;print(x+f'={b}')
Based on @dingledooper's Python 2 answer.
-
1\$\begingroup\$ You can save a byte with some cursed formatting :) \$\endgroup\$emanresu A– emanresu A2022年03月05日 04:12:49 +00:00Commented Mar 5, 2022 at 4:12
SNOBOL4 (CSNOBOL4), (削除) 90 87 (削除ここまで) 84 bytes
N =1
N N =M + (M =N)
OUTPUT =P =DUPL(' ',X) M '+' N '=' M + N
P @X N '=' :(N)
END
Outputs infinitely, but experiences integer overflow after a short while.
Jelly, 22 bytes
+2ÆḞ€DżṖ©;)+=Ɗ6ṁ$®¦F)Y
A full program that accepts an integer and prints the result.
How?
+2ÆḞ€DżṖ©;)+=Ɗ6ṁ$®¦F)Y - Main Link: integer, N
) - for each V in [1..N]:
+2 - V add two
ÆḞ€ - Fibbonacci of each of [1..V+2]
D - to decimal digits -> [[1],[1],[2],...,Digits(Fib(V+2))]
call this FibDigits
Ɗ - last three links as a monad - f(V):
Ṗ - pop -> [1..V-1]
© - (copy this to the register)
)+= - ['+', '=']
; - concatenate -> [1,2,3,...,V-1,'+','=']
call this Fillers
ż - FibDigits zip with Fillers
¦ - sparse application...
® - ...to indices: recall from register -> [1..V-1]
$ - ...action: last two links as a monad:
6 - space character
ṁ - mould like (e.g. [[1,4,4],12] -> [[' ',' ',' '], ' ']
F - flatten
Y - join with newlines
- implicit print
Vyxal, 19 bytes
ÞF3l(:n‛+=Y∑꘍,nhL›+
Outputs infinitely.
ÞF3l # Take the infinite list of Fibonacci numbers, and get overlapping groups of 3
( # Looping over that...
Y∑ # Interleave...
n # The tuple of three numbers
‛+= # With '+='
: ꘍ # Pad that with the correct amount of spaces, without popping the padding amount
, # Print that
+ # Add to the padding amount (initially 0)
nhL› # The length of the first number in the tuple, plus one.
Retina 0.8.2, 55 bytes
1=1
{`.*\+
$.&$*
=
+
\d+.(\d+)
$&=$&$*_1ドル$*_
:`_+
$.&
Try it online! Tries to output indefinitely but runs out of memory after about 50 seconds on TIO. Explanation:
1=1
Initialise the buffer with a hypothetical previous line.
{`
Repeat indefinitely.
.*\+
$.&$*
Replace up to and including the +
with spaces. (This only applies after the first loop.)
=
+
Change the =
into a +
.
\d+.(\d+)
$&=$&$*_1ドル$*_
Append the sum of the two values in unary.
:`_+
$.&
Convert the unary to decimal and output the result.
Python, (削除) 117 (削除ここまで) (削除) 107 (削除ここまで) (削除) 103 (削除ここまで) (削除) 99 (削除ここまで) 88 bytes
p="1=1" while p:=" "*(a:=1+p.find("+"))+eval("f'{%s=}'"%p[a:].replace("=","+")):print(p)
Outputs infinitely.
Can probably be much improved.
-
\$\begingroup\$ How do you use ":=" in a lambda? \$\endgroup\$Fmbalbuena– Fmbalbuena2022年03月04日 21:16:15 +00:00Commented Mar 4, 2022 at 21:16
-
1\$\begingroup\$ @Fmbalbuena What do you mean by "how"? It's the same as anywhere else \$\endgroup\$pxeger– pxeger2022年03月04日 21:19:15 +00:00Commented Mar 4, 2022 at 21:19
05AB1E, (削除) 23 (削除ここまで) 22 bytes
ÌLÅfü3ε„+=.ιJ ̄Oú,yнg>ˆ
-1 byte after being inspired by @JonathanAllan's Jelly answer
Outputs the first \$n\$ lines.
Or a minor 22 bytes alternative:
ÌLÅfDü3„+=δ.ιJs€g>.\ú»
And the infinite sequence would be 22 bytes as well:
∞Åfü3vy„+=.ιJ ̄Oú,yнg>ˆ
Explanation:
Ì # Increase the (implicit) input-integer by 2
L # Pop and push a list in the range [1,input+2]
Åf # Get the 0-based n'th Fibonacci value for each of these
ü3 # Pop and push all overlapping triplets of this list
ε # Foreach over the triplets:
# (implicitly push the current triplet)
„+=.ι # Intersperse it with "+" and "=" delimiters
J # Join this list together to a string
̄ # Push the global_array (empty by default)
O # Sum it together
ú # Pad the string with that many leading spaces
, # Pop and output this line with trailing newline
y # Push the pair again
н # Pop and push its first item
g # Pop and push its length
> # Increase it by 1
ˆ # Pop and add it to the global_array
-
\$\begingroup\$ You might be able to save bytes on the infinite version by porting my Vyxal answer and keeping the padding amount on the stack? \$\endgroup\$emanresu A– emanresu A2022年03月05日 21:47:38 +00:00Commented Mar 5, 2022 at 21:47
-
\$\begingroup\$ @emanresuA I'm afraid not. It would be 22 bytes as well. 05AB1E remembers the last value if it needs an argument and the stack is now empty, so I will have to push an explicit
0
at the start (otherwise it will use the infinite overlapping triplets list for the padding). In addition, I need an explicitD
uplicate within the loop. That, combined with the 1 byte longer Fibonacci builtin compared to your Vyxal answer, is an alternative infinite 22-byter. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2022年03月05日 22:08:30 +00:00Commented Mar 5, 2022 at 22:08
R, 76 bytes
l=b=1
repeat{cat(sprintf("%*d+%d=%d
",l,T,b,s<-T+b));l=l+nchar(b)+1;T=b;b=s}
Outputs infinitely.
If leading whitespace is allowed:
R, (削除) 74 (削除ここまで) 69 bytes
b=1
repeat cat(sprintf("%*d+%d=%d
",F<-F+nchar(+T)+1,T,b,b<-T+(T=b)))
Retina, 54 bytes
K`1+1=2
"$+"+`(\d+)=(\d+)$
$&¶$.%`* 1ドル+2ドル=$.(*_2ドル*
A`$
Try it online! No test suite because of the way the program uses history. Explanation:
K`1+1=2
Replace the input with the first line of the output.
"$+"+`
Repeat n
times.
(\d+)=(\d+)$
Match fn=fn+1
from the previous line.
$&¶$.%`* 1ドル+2ドル=$.(*_2ドル*
Append fn+fn+1=
plus their sum, with the correct amount of indent.
A`$
Since the program started with one line and appended n
lines, it now has one line too many, so delete the last line.
Bash, (削除) 139 (削除ここまで) . . . (削除) 104 (削除ここまで) 103 bytes
_()($[a=b=1];for n in `seq 1ドル`;{ printf "%${s}s";echo -n $a+$b;$[c=a+b,s+=~${#a},a=b,b=c];echo "=$c";})
-
\$\begingroup\$ Is all that whitespace necessary? \$\endgroup\$emanresu A– emanresu A2022年03月05日 05:14:02 +00:00Commented Mar 5, 2022 at 5:14
-
\$\begingroup\$ @emanresuA not sure :) \$\endgroup\$sinvec– sinvec2022年03月05日 05:14:38 +00:00Commented Mar 5, 2022 at 5:14
-
\$\begingroup\$ Removed \$\endgroup\$emanresu A– emanresu A2022年03月05日 05:17:11 +00:00Commented Mar 5, 2022 at 5:17
Haskell, 89 bytes
l!s@[a,b,c]|[x,y,z]<-show<$>s=(l++x++'+':y++'=':z):(' '<$' ':x++l)![b,c,b+c]
f=""![1,1,2]
f
is a stream of lines.
l!
s formats current line and computes next triplet s and leading whitespace l
CJam, (削除) 49 (削除ここまで) (削除) 47 (削除ここまで) 45 bytes
liX:Y;{[ST*X`_,T)+:T;'+Y:K'=XY+:YN]oK:X;(_}g;
C (gcc), (削除) 102 (削除ここまで) (削除) 76 (削除ここまで) 72 bytes
b;s;f(a){for(a=b=1;s=printf("%*d+%d",s,a,b);a=b-a)printf("=%d\n",b+=a);}
Saved a whopping 26 bytes thanks to ceilingcat!!!
Saved 3 bytes thanks to m90!!!
Outputs forever!
-
\$\begingroup\$ A further improvement \$\endgroup\$m90– m902022年03月06日 12:43:18 +00:00Commented Mar 6, 2022 at 12:43
-
\$\begingroup\$ @m90 Nice one - thanks! :D \$\endgroup\$Noodle9– Noodle92022年03月06日 17:07:05 +00:00Commented Mar 6, 2022 at 17:07
Julia 1.0, (削除) 66 (削除ここまで) 65 bytes
f(a=1,b=1,n=0)=print(" "^n,"$a+$b=$(a+b)
")f(b,a+b,n-~ndigits(a))
prints infinitely. To avoid Int64
overflow, replace a=1
with a=big(1)
(+5 bytes)
Python 3, (削除) 146 (削除ここまで) 141 bytes
print("1+1=2")
a=[1,1,2]
w=2
while 1:print(" "*w+str(a[1])+"+"+str(a[2])+"="+str(a[1]+a[2]));del a[0];w+=len(str(a[0]))+1;a.append(a[0]+a[1])
Japt -mR
, (削除) 23 (削除ここまで) 22 bytes
Includes a trailing space on each line of output.
"+= "¬ËiMg°UÃvÈùT±XÊì
"+= "¬ËiMg°UÃvÈùT±XÊì :Implicit map of each U in the range [0,input)
"+= "¬ : Split "+= " to an array of characters
Ë : Map
i : Prepend
°U : Prefix increment U
Mg : Get the Uth Fibonacci number, 0-indexed
à : End map
v : Modify the first element
È : By passing it through the following function as X
ù : Left pad with spaces to length
T± : Increment T (initially 0) by
XÊ : Length of X
à : End modification
¬ : Join
:Implicit output joined with newlines
Scala, (削除) 228 (削除ここまで) (削除) 226 (削除ここまで) 225 bytes
Saved 3 bytes thanks to the comment of @ceilingcat
Golfed version. Try it online!
object Main extends App{def f(n:Int)={def g(a:Int,b:Int,n:Int):List[(Int,Int,Int)]=n match{case 0=>Nil case _=>(a,b,a+b)::g(b,a+b,n-1)};g(1,1,n)};f(20).zipWithIndex.map{case((a,b,c),i)=>(" "*i)+s"$a+$b=$c"}.foreach(println)}
Ungolfed version. Try it online!
object Main extends App {
def formatList(list: List[(Int, Int, Int)]): List[String] = {
list.zipWithIndex.map {
case ((a, b, c), i) => (" " * (2 * i)) + s"$a + $b = $c"
}
}
def fibonacciSeq(n: Int): List[(Int, Int, Int)] = {
def fibHelper(a: Int, b: Int, n: Int): List[(Int, Int, Int)] = n match {
case 0 => Nil
case _ => (a, b, a + b) :: fibHelper(b, a + b, n - 1)
}
fibHelper(1, 1, n)
}
val formatted = formatList(fibonacciSeq(20))
formatted.foreach(println)
}
-
\$\begingroup\$ It looks like
20
is hardcoded into this; you must be able to take the value as input, or output infinitely. \$\endgroup\$Shaggy– Shaggy2023年07月01日 08:33:23 +00:00Commented Jul 1, 2023 at 8:33