0
0

スーパー鳩時計

Last updated at Posted at 2024-01-30

素直に書くと

for x in range(24):
    for y in range(60):
        if ((x + y) % 3 == 0 and (x + y) % 5 == 0) or (x + y) == 0:
            print('FIZZBUZZ')
        elif (x + y) % 3 == 0:
            print('FIZZ')
        elif (x + y) % 5 == 0:
            print('BUZZ')
        else:
            print()

3で割り切れるかつ5で割り切れる数、かもしくは0もx+yでというやつ。
とりあえず倍数をあげてみます
3 6 9 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60
5 10 15 20 25 30 35 40 45 50 55 60

両者に共通する数を抜き出せばいいですね
15 30 45 60だけでした。これは。。。15の倍数ですね。。。

で、0になるのってx+y=0の、x=0、y=0のときだけ。
あれ?0って何で割っても余り0だよね?
(このあと実際にprintして試して0になるのを確認)
ってことは結局15の倍数になるものだけFIZZBUZZってことになります。
うーん数学奥が深い。

for x in range(24):
    for y in range(60):
#        if ((x + y) % 3 == 0 and (x + y) % 5 == 0) or (x + y) == 0:
        if  (x + y) % 15 == 0:
            print('FIZZBUZZ')
        elif (x + y) % 3 == 0:
            print('FIZZ')
        elif (x + y) % 5 == 0:
            print('BUZZ')
        else:
            print()
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0