# A note on originality: FizzBuzz has been solved in hundreds of ways by # thousands of people. Most of the solutions found here have been rediscovered # time and again by various authors. I will endeavor to give credit for # original solutions and new combinations of existing solutions. If you feel # you deserve shared credit for one of these solutions, by all means please # let me know. $:.unshift File.dirname(__FILE__) # Prints numbers from 1 to 100. If the number is divisible by 3, it prints # "Fizz" instead. If divisible by 5, it prints "Buzz" instead. If divisible by # both 3 and 5, it prints "FizzBuzz" instead. def fizzbuzz fizzbuzz_map_with_ternary_modulo_15_3_5 end # The gold standard. Executes in constant time, also has a constant amount of # elegance (where k is quite small) def fizzbuzz_hardcode [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", "Fizz", 52, 53, "Fizz", "Buzz", 56, "Fizz", 58, 59, "FizzBuzz", 61, 62, "Fizz", 64, "Buzz", "Fizz", 67, 68, "Fizz", "Buzz", 71, "Fizz", 73, 74, "FizzBuzz", 76, 77, "Fizz", 79, "Buzz", "Fizz", 82, 83, "Fizz", "Buzz", 86, "Fizz", 88, 89, "FizzBuzz", 91, 92, "Fizz", 94, "Buzz", "Fizz", 97, 98, "Fizz", "Buzz"] end # Uses ternary evaluation to choose element; uses modulo 15 to represent the # combination of moduli 3 and 5. def fizzbuzz_map_with_ternary_modulo_15_3_5 (1..100).map {|i| (i%15).zero? ? "FizzBuzz" : (i%3).zero? ? "Fizz" : (i%5).zero? ? "Buzz" : i } end # From: Dan Uznanski def fizzbuzz_mod15_index (1..100).map {|i| ['fizzbuzz', nil, nil, 'fizz', nil, 'buzz', 'fizz', nil, nil, 'fizz', 'buzz', nil, 'fizz', nil, nil][i % 15] || i } end # From: Arnar Birgisson def fizzbuzz_indirect_index (1..100).map {|i| ['fizzbuzz', 'fizz', 'buzz', i][[i%15,i%3,i%5,0].index(0)] } end # From: Paulo Bonzini def fizzbuzz_25bit_encoding (1..100).map {|a| [a, "Fizz", "Buzz", "FizzBuzz"][(0x1241843 >> ((a % 15) * 2)) & 3] } end # Uses the fact that seed 1781773465 in Ruby's rand will generate the 15-digit # sequence that repeats in the FizzBuzz progression. The premise here is that # we want to cleverly trick rand into delivering a predictable sequence. (It # is interesting to note that we don't actually gain a reduction in # information size. The 15-digit sequence can be encoded as bit pairs and # stored in a 30-bit number. Since 1781773465 requires 31 bits of storage, our # cleverness has actually cost us a bit of storage efficiency. BUT THAT'S NOT # THE POINT!) # # From: David Brady #-- # This sequence adheres to the specific ordering of 0=int, 1=Fizz, 2=Buzz, # 3=FizzBuzz. It may be possible to find a smaller key if the ordering is # changed. There are 24 possible permutations. If we assume that the # permutations are evenly distributed throughout 2**31 space, and about a 50% # probability that this one is "about halfway through", then we can assume # with a decent confidence (say 20-50%) that there is a key somewhere around # 1.4e+9 (below 2**28). It's not much gain but it DOES demonstrate leveraging # rand's predefined sequence to "hide" 30 bits of information in less that 30 # bits of space. # # Result: Yep. The permutation [3,2,0,1] appears at seed 46308667, which can # be stored in 26 bits. def fizzbuzz_rand_sequence15_0123 (0..99).map {|i| srand(1781773465) if (i%15).zero?; [i+1, "Fizz", "Buzz", "FizzBuzz"][rand(4)]} end # Here's a 26-bit seed # From: David Brady def fizzbuzz_rand_sequence15_3201 (0..99).map {|i| srand(46308667) if (i%15).zero?; ["FizzBuzz", "Buzz", i+1, "Fizz"][rand(4)]} end # If you want a 7-digit seed for 0,0,1,0,2,1,0, srand 708; (1..7).map{rand(3)} is your friend. # If you want a 14-digit seed for 0,0,1,0,2,1,0,0,1,2,0,1,0,0, srand 1577453; (1..14).map{rand(3)} is your friend. # Uses the fact that the FizzBuzz sequence is a repeating 15-digit sequence, # and that the 15-digit can further be broken down into a, a', b, where b is # "FizzBuzz" and a' is the reverse of a. # From: David Brady def fizzbuzz_sequence7_reverse i=0; stack = [0, 0, "Fizz", 0, "Buzz", "Fizz", 0] ((stack + stack.reverse << "FizzBuzz")*7).map {|e| i+=1; e.is_a?(String) && e || i }[0..99] end