# Fizz Buzz with unique characters in columns

Inspired by Digits in their lanes and 1, 2, Fizz, 4, Buzz

# Introduction

Your task is to generate exactly the following output:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
FizzBuzz
46
47
Fizz
49
Buzz


# Challenge

This challenge is based on the Fizz Buzz challenge, and here is a recap: output the numbers from 1 to 100 inclusive, each number on its own line, but if the number is a multiple of 3, you should output "Fizz" instead of the original number, if the number is a multiple of 5, you should output "Buzz" instead of the original number. If the number is a multiple of 15, you should output "FizzBuzz" instead of the original number.

However, in addition to the requirement above, you should also indent each line using spaces to make that every column contains unique characters (excluding spaces and newlines) only. The spaces prepended to each line are the minimum required to make all the lines appeared before it (inclusive) has unique characters in each column.

For example, 1,2,Fizz,4 does not need any indentation because they already have unique characters in each column (column 1: 12F4, column2: i, column3: z, column4: z), but when adding Buzz we need to indent by two spaces because otherwise we would have two z's in both the 3rd and the 4th column. Since two spaces is enough to achieve the goal you should not indent it by three spaces. 7 and 8 does not need any indentation but when outputting 11 we need to indent by one space because the 1st column already has a 1. 13 then needs to be indented by three spaces because now 1st, 2nd and 3rd column all have a 1. Indentation for the remaining lines follow the same rule.

To make the challenge simpler, The upper limit has been changed to 50.

# Specs

• You can write a program or a function. Neither of them should take any non-empty input. Taking an empty input is fine.

• Since this is a KC challenge you need to produce the output as specified in the Introduction section. A single trailing newline is fine. No heading newlines or extra heading spaces. No extra trailing spaces for each line.

• Your program can exit with error or have non-empty STDERR output as long as STDOUT conforms to the specification.

• This is intra-language , the program with lowest number of bytes wins in its language.

• Default loopholes apply.

• The given output doesn't exactly match the spec, e.g. lines 12, 20, 35 and 50. Commented Mar 28, 2018 at 4:10
• But the second character in the first two lines is carriage return. Commented Mar 28, 2018 at 14:53
• I have excluded spaces, now I should exclude newlines as well. Commented Mar 28, 2018 at 15:23

# Python 2, 127 bytes

i=0;exec"print ord('<<<<>@<<BD=F?@HABJCNP=@RT?VABXCBZ<^=>bdDf>?hBCjEn'[i])%60*' '+(i%3/2*'Fizz'+i%5/4*'Buzz'or-~i);i+=1;"*50


Try it online!

A fifty-byte lookup table seems to hurt the code size less than the logic necessary to track which characters have occurred in each column.

• Logic would have been better if we were required to print up to 100 instead... Commented Mar 28, 2018 at 22:55

# Python 2, 167166163161 157 bytes

a=eval([{0}]*99);i=0
exec"f=i%3/2*'Fizz'+i%5/4*'Buzz'ori+1;i+=1;g=0\nwhile any(b>{c}for b,c in zip(a[g:],f)):g+=1\nmap(set.add,a[g:],f);print' '*g+f;"*50


Try it online!

Edits:

• while is shorter than for..range() by 1 byte.
• Thanks to @ovs for shaving off 3 bytes. I always forget exec...
• Adapted i%3/2 trick from Lynn's answer (-2 bytes).
• @Lynn suggested a=map(set,[[]]*99), but I found another way using eval and repr with same bytes (-4 bytes).

Use a list of sets to track the chars used for each column, and set inequality for membership. The rest follows the exact spec given.

# C (gcc), 145 144 bytes (143 for hex)

i;main(){for(;i++<50;printf("%*s%s%.d\n","000402800:81>34@56B7BH14JH3N56P76R0RX12ZX8^2367b9b"[i]-48,i%3?"":"Fizz",i%5?"":"Buzz",i%3*i%5?i:0));}


Try it online!

0000h: 69 3B 6D 61 69 6E 28 29 7B 66 6F 72 28 3B 69 2B ; i;main(){for(;i+
0010h: 2B 3C 35 30 3B 70 72 69 6E 74 66 28 22 25 2A 73 ; +<50;printf("%*s
0020h: 25 73 25 2E 64 5C 6E 22 2C 22 FE FE FE 02 FE 00 ; %s%.d\n","......
0030h: 06 FE FE 08 06 FF 0C 01 02 0E 03 04 10 05 10 16 ; ................
0040h: FF 02 18 16 01 1C 03 04 1E 05 04 20 FE 20 26 FF ; ........... . &.
0050h: 63 28 26 06 2C 00 01 2E 04 05 30 07 30 22 5B 69 ; c(&.,.....0.0"[i
0060h: 5D 2B 32 2C 69 25 33 3F 22 22 3A 22 46 69 7A 7A ; ]+2,i%3?"":"Fizz
0070h: 22 2C 69 25 35 3F 22 22 3A 22 42 75 7A 7A 22 2C ; ",i%5?"":"Buzz",
0080h: 69 25 33 2A 69 25 35 3F 69 3A 30 29 29 3B 7D    ; i%3*i%5?i:0));}


# Ruby, 129 bytes

puts (1..50).map{|n|" "*(".<<<<>@<<BD=F?@HABJCNP=@RT?VABXCBZ<^=>bdDf>?hBCjEn"[n].ord%60)+("FizzBuzz
"[i=n**4%-15,i+13]||n.to_s)}


Try it online!

Double credit goes to Lynn here, for the lookup table approach and the fizzbuzz algorithm.

The FizzBuzz algorithm is very interesting, and it hinges on the remarkable coincidence that all positive, non-composite numbers less than 15 (other than 3 and 5), when raised to the 4th power, are 1 more than a multiple of 15. In fact:

 n     n**4  n**4%15  n**4%-15
1        1     1       -14
2       16     1       -14
3       81     6        -9
4      256     1       -14
5      625    10        -5
6     1296     6        -9
7     2401     1       -14
8     4096     1       -14
9     6561     6        -9
10    10000    10        -5
11    14641     1       -14
12    20736     6        -9
13    28561     1       -14
14    38416     1       -14
15    50625     0         0


The values 3**4%15 and 5**4%15 are exactly 4 apart: the length of the string "Fizz". We can exploit this by using them to index from the end of a string, at least 9 characters in length. Multiples of 3 will index from the beginning of the string, and multiples of 5 will index from 5 characters from the end. Every other number will attempt to index from before the beginning of the string, and fail, returning nil. Then 15, of course, indexes from the 0th character. The fact that "FizzBuzz" is only 8 characters long is a small obstacle; we use a newline character to pad it, which will later be ignored by puts.

It's possible that the lookup table can be out-golfed by a more procedural approach, but my attempt was in the neighborhood of 190 bytes.

• Interesting. It should be noted that the fact all numbers coprime to 15 when raised to 4th power equal to 1 modulo 15 can be derived from Fermat's little theorem. Commented Mar 28, 2018 at 21:45

# [JavaScript (Node.js) REPL], 144 bytes

(f=(i,s=[['Fizz'][i%3]]+[['Buzz'][i%5]]||i+[],b=i>1?f(i-1):[])=>[...s].some((p,j)=>b.some(w=>w[j]==p&0!=p))?f(i,' '+s):b.push(s)&&b)(50).join




Try it online!

Warning program itself runs unacceptable time

f=(a=n=[],s=${b=++n%5?'':'Buzz',n%3?b||n:'Fizz'+b} )=>n>50?'':a.some(x=>[...x].some((c,i)=>c!=0&c==s[i]))?f(a,' '+s):s+f([s,...a])  Try it online! • Your answer doesn’t seem to be the same as the TIO link – Jo King Commented Mar 28, 2018 at 11:43 • @JoKing TIO outputs array, and i don't know whether it's allowed – l4m2 Commented Mar 28, 2018 at 12:36 # Java (JDK 10), 185 bytes v->{for(int n=0,l;n<50;System.out.printf((l>0?"%"+l:"%")+"s%s%n","",(n%3<1?"Fizz":"")+(n%5<1?"Buzz":n%3<1?"":n)))l="####%'##)+$-&'/()1*57$'9;&=()?*)A#EG$%IK+M%&O)*Q,U#".charAt(n++)-35;}


Try it online!

# Haskell, 190 187 186 178 176 bytes

unlines$foldl(\a x->a++[[z|z<-iterate(' ':)x,all(\m->null[p|(p,q)<-mzipz,p==q&&p>' '])a]!!0])[]$h<$>[1..50] a%b=amodb<1 h n|n%15="FizzBuzz"|n%3="Fizz"|n%5="Buzz"|1<2=show n  Try it online! The slightly more readable (and annotated) version: -- check if a is evenly divisible by b a%b=amodb<1 -- produce correct FizzBuzz output for a number h n|n%15="FizzBuzz"|n%3="Fizz"|n%5="Buzz"|1<2=show n -- test if all chars distinct between two strings x#y=null[a|(a,b)<-xzipy,a==b&&a>' '] -- given a new string and all previous strings -- shift the new string to the right until all -- chars are distinct x!y=[z|z<-iterate(' ':)y,all(z#)x]!!0 g=h<$>[1..50]
f'=foldl step[]g
where step acc x = acc++[acc!x]


Edit: I ended up inlining some functions in the golfed version to save more bytes.

• @Laikoni Right. Fixed. Commented Mar 28, 2018 at 11:06

# Jstx, 122 bytes

◄50-☺6*ø($♥:ø↕♂Fizz♀☺(◙$♣:ø↕♂Buzz♀6☺(◙"ø↕\$6◙♂<<<<>@<<BD=F?@HABJCNP=@RT?VABXCBZ<^=>bdDf>?hBCjEn'[i])%60*♀&P◄59▼ö,► 7.☻a0.2


Try it online!

# Deadfish~, 3041 bytes

{i}c{iiii}cdc{dddd}ic{iiii}iiic{dddd}dddcc{iiiiii}iiic{iiii}dddciiiiicdddc{d}iiic{i}dc{d}dddcdc{{d}iii}iic{iiiiii}iiiiiic{ii}iiic{{d}i}ic{iii}iiiiiic{iiii}dddcddciic{i}icdc{{d}ii}dddc{{i}ddd}iiiciiiiic{{d}ii}iic{{i}dd}iiiic{d}ddcdddciiiic{i}dc{{d}ii}ddc{{i}ddd}iiiiiic{d}dc{i}iiic{d}ic{i}iiiic{{d}ii}dddc{iiiiii}iiiiic{i}iiic{d}c{{d}iii}iic{ii}dddcdddddc{d}ddc{ii}ddcddddddc{d}ddc{iiii}ddc{iii}iiiiic{ii}dddcc{{d}ii}iic{d}ddc{ii}c{d}iic{d}ddc{iii}iiiic{iiiii}iciiiiicc{{d}}{d}ddc{iiiiii}iiic{iiii}dddciiiiiicddcdddc{d}dc{ii}dddc{dd}iic{ii}dddc{d}dciiiiiicdc{{d}}cc{{i}dd}dc{ii}iiciiiiiicdddc{{d}ii}ddc{{i}dd}iiiic{dd}ic{ii}ddc{d}iic{{d}iii}dddddc{{i}ddd}iiic{i}c{{d}ii}dddc{{i}dd}iiiicdddddc{{d}ii}ic{{i}ddd}icddc{i}dc{d}ic{i}iiic{dd}iiic{ii}dc{d}dddddc{{d}iii}ic{{i}ddd}dc{ii}dc{dd}dddciic{ii}dddc{d}iic{i}iiic{{d}i}ic{{i}dd}iiiic{d}ddcdddc{{d}iii}ic{{i}ddd}c{i}dcdddcciiic{i}ddc{d}ddddciiiiic{d}iiic{{d}iii}dc{{i}dd}dciiiiiicdcddddciiiiicdc{dddddd}iic{ddddd}iicc{iiii}dc{dddd}ic{iiii}c{dddd}c{iiiiii}c{iii}iiiiic{ii}dddcc{{d}}{d}ddc{iiii}iic{dddd}ddc{ii}iicc{iii}iiiic{iiiii}iciiiiicc{{d}}{d}ddc{ii}iicccc{iiii}ddc{iii}iiiiic{ii}dddcc{{d}}{d}ddc{iiii}iiiiic{dddd}dddddc{iiii}iiiiiic{dddd}ddddddc{ii}iicccccc{iiii}ddc{iii}iiiiic{ii}dddcc{{d}}{d}ddc{ii}iicccccccc{iii}iiiic{iiiii}iciiiiicc{{d}}{d}ddc{ii}iic{ii}dddcc{dddd}ic{ii}ii{c}{iiii}ddc{iii}iiiiic{ii}dddcc{{d}}{d}ddc{ii}iiccc{ii}dddciic{dddd}dc{ii}iicccc{ii}dddciiic{dddd}ddc{ii}ii{c}cc{iiii}ddc{iii}iiiiic{ii}dddcc{ddddd}ddddddc{iiiii}iciiiiicc{{d}}{d}ddc{ii}iiccccc{ii}dddciiiiic{dddd}ddddc{ii}iicccccc{ii}dddciiiiiic{dddd}dddddc{ii}ii{c}cccc{iiii}ddc{iii}iiiiic{ii}dddcc{{d}}{d}ddc{ii}iiccccccc{ii}dddc{i}ddc{ddddd}iiic{ii}ii{c}cccccccc{iii}iiiic{iiiii}iciiiiicc{{d}}{d}ddc{ii}ii{cc}{iiii}ddc{iii}iiiiic{ii}dddcc{{d}}{d}ddc{ii}iic{ii}ddcc{dddd}c{ii}iicccc{ii}ddcic{dddd}dc{ii}ii{cc}cc{iiii}ddc{iii}iiiiic{ii}dddcc{{d}}{d}ddc{ii}ii{cc}cccc{iii}iiiic{iiiii}iciiiiicc{{d}}{d}ddc{ii}iiccc{ii}ddciiiic{dddd}ddddc{ii}ii{cc}cccccc{iiii}ddc{iii}iiiiic{ii}dddcc{{d}}{d}ddc{ii}iiccccc{ii}ddciiiiiic{dddd}ddddddc{ii}iicccccc{ii}ddc{i}dddc{ddddd}iiic{ii}ii{cc}cccccccc{iiii}ddc{iii}iiiiic{ii}dddcc{ddddd}ddddddc{iiiii}iciiiiicc{{d}}{d}ddc{ii}iiccccccc{ii}dcddc{dddd}ic{ii}iicccccc{ii}dcdc{dddd}c{ii}ii{cc}{c}{iiii}ddc{iii}iiiiic{ii}dddcc{{d}}{d}ddc{iiii}icic{dddd}ddc{ii}ii{cc}{c}cccc{iii}iiiic{iiiii}iciiiiicc{{d}}{d}ddc{ii}ii{cc}{c}cccccc{iiii}ddc{iii}iiiiic{ii}dddcc{{d}}{d}ddc{ii}iic{ii}dciiiic{dddd}dddddc{ii}iicc{ii}dciiiiic{dddd}ddddddc{ii}ii{cc}{c}cccccccc{iiii}ddc{iii}iiiiic{ii}dddcc{{d}}{d}ddc{ii}ii{cc}{cc}{iii}iiiic{iiiii}iciiiiicc{{d}}{d}ddc{ii}iicccccccc{ii}cdddc{dddd}ic{ii}ii{cc}{cc}cc{iiii}ddc{iii}iiiiic{ii}dddcc{{d}}{d}ddc{ii}iicc{ii}cdc{dddd}dc{ii}iiccc{ii}cc{dddd}ddc{ii}ii{cc}{cc}cccc{iiii}ddc{iii}iiiiic{ii}dddcc{ddddd}ddddddc{iiiii}iciiiiicc{{d}}{d}ddc{ii}iicccccc{ii}ciic{dddd}ddddc{ii}iiccccccc{ii}ciiic{dddd}dddddc{ii}ii{cc}{cc}cccccc{iiii}ddc{iii}iiiiic{ii}dddcc{{d}}{d}ddc{ii}iiccccccccc{ii}ciiiiic{ddddd}iiic{ii}ii{cc}{cc}{c}{iii}iiiic{iiiii}iciiiiicc{{d}}{d}ddc