-1

I'm trying to find a way to do the FizzBuzz problem (print all numbers between 1 and 100, print Fizz if it's a multiple of 3, print Buzz if it's a multiple of 5 and FizzBuzz if it's both) using arithmetic only.

It's fairly easy if you do it using the traditional 3 and 5 because you can use this method to return 0 if it is a multiple of 3:

(i*i)%3

and this can be implemented to print the first part of "FizzBuzz"

print("FizzBuzz"[((i*i)%3)*4:4] or i)
#It's multiplied by 4 so that if it isn't a mutliple of 3 it tries to print
#"FizzBuzz"[4:4] which is blank, so it print i instead.

A similar method can be done with multiples of 5

(i^4)%5

And to make this a functional FizzBuzz we need to convert 0 into 8 and 1 into 4 by:

8 - ((-i^4)%5)

This is now a functional FizzBuzz in python:

for i in range(1,101):
    print("FizzBuzz"[((i*i)%3)*4:8 - ((-i**4)%5)] or i)

I have discovered there is a way to get a 0 or a 1 depending on whether a number is a multiple of a desired number like this:

result = (number ^ (desired_number - 1)) % desired_number

But this rule only works for prime numbers and if you tried this with any other number the entire idea falls apart. Can a similar function be made for non-primes or is this something that applies to primes only?

Auh
  • 145
  • 11

2 Answers2

1

This is indeed the small theorem of Fermat.

There is a generalization using Eulers Totient function phi in that

a^phi(m) ==1 mod m

if a and m are relatively prime.

As phi(15)=phi(3)*phi(5)=(3-1)*(5-1)=8, the remainders of a^8 mod 15 for a=0,1,2,...,14 are

0,1,1,6,1,10,6,1,1,6,10,1,6,1,1.
Lutz Lehmann
  • 25,219
  • 2
  • 22
  • 51
0

As an alternative, here is a very different approach. I cannot write Python, so this is pseudocode, with no explicit if statements:

method FizzBuzz

  array Fizz3 = ["", "", "Fizz"];          // 3 elements.
  array Buzz5 = ["", "", "", "", "Buzz"];  // 5 elements.

  for i = 0 to 99
    print((i+1) + ": " + Fizz3[i % 3] + Buzz5[i % 5]);
  end for

end method FizzBuzz

I assume zero based arrays.

rossum
  • 15,344
  • 1
  • 24
  • 38
  • That's OK; I had fun thinking it up. :) – rossum Oct 27 '16 at 16:09
  • Also this would print every number, it doesn't print Fizz or Buzz only, it print 3:Fizz, 5:Buzz, 15:FizzBuzz which it isn't supposed to do. – Auh Oct 27 '16 at 16:09
  • I just tested this and you actually over complicated it. You don't even need to rotate the arrays as long as you position fizz or buzz as the first element of the array. (because you're referencing i%3 or i%5 as the position in the array) If you would rather have the arrays rotate you need to reference the last position in the array every time. – Auh Oct 27 '16 at 16:15
  • I realised the same thing. See my edit. Great minds think alike. Fools seldom differ. Take your pick. :) – rossum Oct 27 '16 at 16:16