How did I end up with this FizzBuzz?

21

4

FizzBuzz is so simple, bet you can do it backwards. In this challenge, you will be given the length of the FizzBuzz string and must give the positive integer that produced that string.

Description

To break this down, a FizzBuzz string for n is generated by the following algorithm.

Start with an empty string and, for every i=1..n (inclusive):

  1. If i is divisible by 3 and by 5, append FizzBuzz to the string.
  2. If i is just divisible by 3 append Fizz.
  3. If i is just divisible by 5 append Buzz.
  4. If i is divisible by neither, append the decimal representation of i.

So for example FizzBuzz(15) is the following:

12Fizz4BuzzFizz78FizzBuzz11Fizz1314FizzBuzz

You will be given Length(FizzBuzz(n)) and must determine n. You may assume that the input is positive and is always going to be the length of some FizzBuzz string.

Rules

Your solution may a complete program or a function definition in any standardly acceptable language. Your program/function may take in arguments and return answers in any standardly accepted way. Standard loopholes are forbidden.

You may assume that the input is positive and valid (describes the length of some FizzBuzz string) and is smaller than the largest integer representable natively in your language.

This is code golf, so shortest byte-count wins.

Examples

Here are some example cases

Length(FizzBuzz(n)) -> n
1                   -> 1
6                   -> 3
15                  -> 6
313                 -> 100
3677                -> 1001

Edit

Fixed last test case. Thanks @SteadyBox.

walpen

Posted 2017-02-21T20:27:33.487

Reputation: 3 237

Argh! I tried to do recursion but my numbers were too big... – 0WJYxW9FMN – 2017-02-21T21:09:53.237

Related. Related. – Digital Trauma – 2017-02-21T21:19:14.397

3@Toto How is this a duplicate? – AdmBorkBork – 2017-02-22T13:30:59.683

1@Toto This is not at all a duplicate. Maybe you should read up on what being a duplicate means. – mbomb007 – 2017-02-22T14:34:50.753

Answers

8

Jelly,  16  14 bytes

2 bytes saved using more recent language features ) for µ€ and Ä for +\

3,5ḍS×4oDL$)Äi

Try it online! or see the test cases.

How?

Builds a list of the lengths of every item from 1 to the input, reduces by addition and then finds the one-based index of the input in the list. (This also means an invalid input results in 0, "not in list").

3,5ḍS×4oDL$)Äi - Main link: theLength
           )    - perform the chain to the left for each (€) in
                     implicit range from 1 to the input and
                     pass the result into the monadic chain (µ) to the right
3,5            - 3 paired with 5: [3,5]
   ḍ           - divides?  for a multiple of 15 [1,1]; sum = 2; times 4 = 8
    S          - sum       for a multiple of  5 [0,1]; sum = 1; times 4 = 4
     ×4        - times 4   for a multiple of  3 [1,0]; sum = 1; times 4 = 4
                           for none of those    [0,0]; sum = 0; times 4 = 0
          $    - last two links as a monad
        D      -     to decimal digit list
         L     -     length - e.g. 313 -> [3,1,3] -> 3
       o       - logical or: replace a 0 with the decimal length, keep the 4s and 8s
            Ä  - reduce with addition: e.g. [1,1,4,1, 4, 4, 1, 1, 4, 4, 2, 4, 2 ,2, 8]
                                         -> [1,2,6,7,11,15,16,17,21,25,27,31,33,35,43]
             i - index of theLength in that list (e.g. 15 is at index 6)

Jonathan Allan

Posted 2017-02-21T20:27:33.487

Reputation: 67 804

11

C, 81 78 bytes

l,i;f(n){for(l=i=0;l<n;l+=++i%3?i%5?snprintf(0,0,"%d",i):4:i%5?4:8);return i;}

68 bytes if you don't mind converting to double and back:

l,i;f(n){for(l=i=0;l<n;l+=++i%3?i%5?log10(i)+1:4:i%5?4:8);return i;}

Steadybox

Posted 2017-02-21T20:27:33.487

Reputation: 15 798

Is "return i" even required, when "i" is a global variable?-) And you could replace that long snprintf call with a log10(i)+1, if that compiles and is allowed... It worked for me with gcc -lm – Rennex – 2017-02-23T04:47:53.997

@Rennex The return i; is needed, because that is a standardly accepted way to output in code golf whereas only modifying a global variable is not. I considered using log10(i)+1, but I thought it might cause some issues because of converting to double and back (e.g. pow(i) is not reliable with integers). It seems now though that it works fine for all positive values that an int can represent, so I could probably use it. (With values larger than a simple int can hold, it does fail sometimes, but that doesn't matter here.) – Steadybox – 2017-02-23T15:07:29.770

Hmm, OK. I'm new to this code golf, but I looked at the rules link in the question, and it says "Functions may output by modifying their arguments or writing to out arguments". Doesn't that mean that at least a result pointer argument could be used? – Rennex – 2017-02-23T17:25:36.827

@Rennex Yeah, I guess I could take the n in as a pointer and then just modify the value it's pointing to at the end, but that would require more code at the call site in order to be able to print the value, so it feels a bit like cheating to me. – Steadybox – 2017-02-23T21:32:46.333

6

MATL, 31 28 27 bytes

`@:tI5h!\XJA)VXznJ~z4*+G-}@

Try it online!

Explanation

`        % Do...while
  @:     %   Push array [1 2 ...k], where k is iteration index
  t      %   Duplicate  
  I5h!   %   Push column vector [3; 5]
  \      %   Modulo, with broadcast. Gives 2 × k matrix
  XJ     %   Copy into clipboard J
  A      %   Row vector that contains true for columns that contain two nonzeros
  )      %   Index with that vector. This keeps numbers that are non-fizz/buzz
  V      %   Convert to string. This inserts spaces between numbers
  Xzn    %   Number of nonspace characters
  J      %   Push 2 × k matrix resulting from modulo operation again
  ~z     %   Number of zeros
  4*     %   Multiply by 4. Gives number of characters corresponding to fizz/buzz
  +      %   Add
  G-     %   Subtract input. This is the loop condition: exit if 0
}        % Finally (execute right before exiting loop)
  @      %   Push current iteration index
         % End (implicit)
         % Display (implicit)

Luis Mendo

Posted 2017-02-21T20:27:33.487

Reputation: 87 464

4

Mathematica, 67 bytes

(For[n=s=0,s<#,s+=Tr[4Boole[{3,5}∣++n]]/. 0:>IntegerLength@n];n)&

This is both faster and shorter than my initial solution:

1//.x_/;Sum[Tr[4Boole[{3,5}∣n]]/. 0:>IntegerLength@n,{n,x}]!=#:>x+1&

or my desperate attempt to shorten it:

(s=0;1)//.x_/;(s+=Tr[4Boole[{3,5}∣x]]/. 0:>IntegerLength@x)!=#:>x+1&

Explanation

Standard For loop which increments n until s := Length(FizzBuzz(n)) is at least equal to the input #. The only interesting bit is how I calculate the length of the (n+1)-th term of the FizzBuzz sequence

                ++n                           Preincrement n
          {3,5}∣                              Test for divisibility by 3 and 5 (returns a list)
    Boole[         ]                          Convert True to 1 and False to 0
   4                                          Multiply by 4
Tr[                 ]                         Sum
                     /.                       Replace
                        0                     0 (leading space is necessary or it thinks we are dividing by 0.0)
                         :>                   with
                           IntegerLength@n    the number of digits in n

ngenisis

Posted 2017-02-21T20:27:33.487

Reputation: 4 600

3

MATL, 31 30 28 bytes

:tI5h!\~s4*t~b10&YlkQ*+YsG=f

Uses the same idea as Jonathan Allen's Jelly solution.

Try it on matl.suever.net!

B. Mehta

Posted 2017-02-21T20:27:33.487

Reputation: 763

Down to 28 now! :-P I think our approaches are more similar now – Luis Mendo – 2017-02-21T22:33:03.537

Ah, good job! Yeah, looks like it :) – B. Mehta – 2017-02-21T22:36:38.933

3

Java 8, 100 97 bytes

Golfed:

l->{int i=0;for(String s="";s.length()<l;)s+=++i%15<1?"12345678":i%5<1||i%3<1?"1234":i;return i;}

Ungolfed:

import java.util.function.*;

public class HowDidIEndUpWithThisFizzBuzz {

  public static void main(String[] args) {
    for (final int[] data : new int[][] { { 1, 1 }, { 6, 3 }, { 15, 6 },
        { 313, 100 }, { 3677, 1001 } }) {
      final int fizzBuzzLength = data[0];
      final int expected = data[1];
      final int actual = f(l -> {
        int i = 0;
        for (String s = ""; s.length() < l;) {
          s += (++i % 15 < 1 ? "12345678" : (i % 5 < 1 || i % 3 < 1 ? "1234" : i));
        }
        return i;
      } , fizzBuzzLength);
      System.out.println("Length(FizzBuzz(n)) -> " + fizzBuzzLength);
      System.out.println("Expected            -> " + expected);
      System.out.println("Actual              -> " + actual);
      System.out.println();
    }

  }

  private static int f(IntFunction<Integer> function, int fizzBuzzLength) {
    return function.apply(fizzBuzzLength);
  }
}

Output:

Length(FizzBuzz(n)) -> 1
Expected            -> 1
Actual              -> 1

Length(FizzBuzz(n)) -> 6
Expected            -> 3
Actual              -> 3

Length(FizzBuzz(n)) -> 15
Expected            -> 6
Actual              -> 6

Length(FizzBuzz(n)) -> 313
Expected            -> 100
Actual              -> 100

Length(FizzBuzz(n)) -> 3677
Expected            -> 1001
Actual              -> 1001

user18932

Posted 2017-02-21T20:27:33.487

Reputation:

2

JavaScript (ES6), 62 57 bytes

f=(n,k=0)=>n?f(n-(++k%3?k%5?`${k}`.length:4:k%5?4:8),k):k

Test cases

f=(n,k=0)=>n?f(n-(++k%3?k%5?`${k}`.length:4:k%5?4:8),k):k

console.log(f(1   )); // -> 1
console.log(f(6   )); // -> 3
console.log(f(15  )); // -> 6
console.log(f(313 )); // -> 100
console.log(f(3677)); // -> 1001

Arnauld

Posted 2017-02-21T20:27:33.487

Reputation: 111 334

Alternative expression with the same length: (!(++k%3)+!(k%5)<<2||\${k}`.length)`. – Neil – 2017-02-22T08:44:13.690

2

Javascript (ES6), 56 bytes

f=(x,s=i=0)=>s[x]?i:f(x,s+[++i%3?i%5?i:1e3:i%5?1e3:1e7])
<!-- snippet demo: -->
<input list=l oninput=console.log(f(this.value))>
<datalist id=l><option value=1><option value=6><option value=15><option value=313><option value=3677></datalist>

nderscore

Posted 2017-02-21T20:27:33.487

Reputation: 4 912

2

Python 3, 78 bytes

f=lambda i,n=1,s=0:~-n*(s==i)or f(i,n+1,s+(4*((n%3<1)+(n%5<1))or len(str(n))))

Recursive function. Will need the recursion limit increased for any result above 1000.

Explanation:

# i = length of final string
# n = current number in sequence, starting with 1
# s = length of current string, starting with 0
f=lambda i,n=1,s=0: \

# if s==1, this will evaluate to n+1, which is NOT 0, and will return
# else, it will evaluate to (n+1)*0, and trigger the second half of the OR clause
~-n*(s==i)or \

# recursively call the next iteration, with the next number in the sequence
f(i,n+1, \ 

# increase s by 4 if Fizz or Buzz, 8 if FizzBuzz, or len(n) if number
s+(4*((n%3<1)+(n%5<1))or len(str(n))))

Triggernometry

Posted 2017-02-21T20:27:33.487

Reputation: 765

1

Python, 93 bytes

def g(n,c=0,a=[4,0]):
 while n:c+=1;s=a[c%3>0]+a[c%5>0];s+=(s<1)*len(str(c));n-=s
 return c

user65823

Posted 2017-02-21T20:27:33.487

Reputation: 21

1

Java 8, 95 93 bytes

l->{int j=0,i=0;for(;j<l;)j+=++i%15<1?8:i%3<1||i%5<1?4:Math.floor(Math.log10(i)+1);return i;}

This is the optimized version of @Snowman's answer

Roman Gräf

Posted 2017-02-21T20:27:33.487

Reputation: 2 915

This returns incorrect results for me on the final two test cases: 75 instead of 100, and 686 instead of 1001. – None – 2017-02-22T19:09:47.167

1

k, 33 bytes

{1+&x=+\{(#$x;4;8)+/~3 5!'x}'1+!x}

Brief (python-ish) explanation:

{                                } / function(x):
                             1+!x  /   array from 1 to x, inclusive
                            '      /   for y in array:
        {                  }       /     function(y):
         (#$x;4;8)                 /       yield [ len(str(y), 4, 8 ][
                  +/~3 5!'x        /         sum([not(y mod 3), not(y mod 5)])
                                   /       ]
      +\                           /   cumulative sum of result of for loop
 1+&x=                             /   get index of x in cumulative sum, add one

Example using kmac 2016.06.28:

 f:{1+&x=+\{(#$x;4;8)+/~3 5!'x}'1+!x}
 ,/f'1 6 15 313 3677
1 3 6 100 1001

zgrep

Posted 2017-02-21T20:27:33.487

Reputation: 1 291

Welcome to Programming Puzzles & Code Golf! Just so you know, the downvote was cast automatically by the Community user when the answer was edited. I consider this a bug.

– Dennis – 2017-02-22T19:51:45.743

1

dc, 76 70 bytes

?sa0dsbsc[lc4+sc]sh[lbZ+sc]so[lcdlb1+ddsb3%0=h5%0=hlc=olcla!=y]dsyxlbp

Try it online!

R. Kap

Posted 2017-02-21T20:27:33.487

Reputation: 4 730

1

Ruby, 69 66 bytes

->n{i=0;(i+=1;n-=i%3>0?i%5>0?i.to_s.size: 4:i%5>0?4:8)while n>0;i}

Originally, I was avoiding the nested ternary operator monstrosity and got down to 69 bytes:

->n{i=0;(i+=1;n-=(x=[i%3,i%5].count 0)>0?4*x:i.to_s.size)while n>0;i}

Rennex

Posted 2017-02-21T20:27:33.487

Reputation: 111

1

Groovy, 76 bytes

def f(n){i=0;for(s='';s.size()<n;)s+=++i%15<1?"1"*8:i%5<1||i%3<1?"1"*4:i;i;}

Mostly the same as @Snowman's answer, but uses some Groovy magic/differences to cut down on the byte count.

TheJizel

Posted 2017-02-21T20:27:33.487

Reputation: 121

0

Perl 6, 55 52 bytes

{1+first $_,:k,[\+] map {4*($_%%3+$_%%5)||.chars},1..*}

{(0,{my \i=++$;$_+(4*(i%%3+i%%5)||i.chars)}...$_)-1}

Try it online!

How it works

{                                                  }  # A lambda.
  0                                                   # Start with 0.
   ,{                                     }           # Use the iteration formula...
     my \i=++$;                                       #   Fetch current index.
               $_+(                      )            #   Last element plus:
                   4*(i%%3+i%%5)                      #     Fizz/Buzz/FizzBuzz length,
                                ||i.chars             #     or number length.
                                           ...$_      # ...until the input is reached.
 (                                              )-1   # Sequence length minus 1.

smls

Posted 2017-02-21T20:27:33.487

Reputation: 4 352

0

Japt, 20 bytes

@µ35ìx_XvZÃ*4ªXìÊ}f1

Try it

@µ35ìx_XvZÃ*4ªXìÊ}f1     :Implicit input of integer U
@                        :Function taking an integer X as argument
 µ                       :  Decrement U by
  35ì                    :    Digit array of 35
     x                   :    Reduce by addition
      _                  :    After passing each Z through the following function
       XvZ               :      Is X divisible by Z?
          Ã              :    End reduce
           *4            :    Multiply by 4
             ª           :    Logical OR with
              Xì         :      Digit array of X
                Ê        :      Length
                 }       :End function
                  f1     :First integer >=1 that returns a falsey value (i.e, 0) when passed through that function

Shaggy

Posted 2017-02-21T20:27:33.487

Reputation: 24 623

0

Perl 5 -p, 48 bytes

$\++;($_-=4*(!($\%3)+!($\%5))||length$\)&&redo}{

Try it online!

Xcali

Posted 2017-02-21T20:27:33.487

Reputation: 7 671

0

C (gcc), 68 bytes, stdout-spamming

  • Thanks to H.PWiz for this approach, saving six bytes.
f(n,j,r){for(r=j=0;!r;r=!n*j)n-=++j%3?j%5?printf("%d",j):4:j%5?4:8;}

Try it online!


C (gcc), 74 bytes

f(n,j,r){for(r=j=0;!r;r=!n*j)n-=++j%3?j%5?snprintf(0,0,"%d",j):4:j%5?4:8;}

Try it online!

Jonathan Frech

Posted 2017-02-21T20:27:33.487

Reputation: 6 681

0

05AB1E, 17 bytes

Lε35SÖ4*OygM}.¥sk

Try it online or verify all test cases.

Explanation:

L          # Create a list in the range [1, (implicit) input]
           #  i.e. 15 → [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
 ε         # Map each value to:
  35S      #  Push 35 as digit list: [3,5]
     Ö     #  Check if the current value is divisible by these (1 if truthy; 0 if falsey)
      4*   #  Multiply both by 4
        O  #  And take the sum of that
           #   i.e. 2 → [0,0] → [0,0] → 0
           #   i.e. 9 → [1,0] → [4,0] → 4
           #   i.e. 10 → [0,1] → [0,4] → 4
           #   i.e. 15 → [1,1] → [4,4] → 8
  yg       #  Push the current value again, and pop and push it's length
           #   i.e. 2 → 1
           #   i.e. 15 → 2
  M        #  And then push the largest value on the stack
           #   i.e. 0 and 1 → 1
           #   i.e. 8 and 2 → 8
 }.¥       # After the map: undelta the list (starting from 0)
           #  i.e. [1,1,4,1,4,4,1,1,4,4,2,4,2,2,8]
           #   → [0,1,2,6,7,11,15,16,17,21,25,27,31,33,35,43] 
    sk     # Swap to get the (implicit) input, and get its 0-based index in the list
           #  i.e. 15 → 6
           # (after which the result is output implicitly)

Kevin Cruijssen

Posted 2017-02-21T20:27:33.487

Reputation: 67 575