LoginSignup
1
1

More than 3 years have passed since last update.

プログラマ脳を鍛える数学パズルQ01「10進数で回文」

Last updated at Posted at 2020-03-31

Code

def kai(i): #回文かどうか判定する
    return str(i)[::-1] == str(i)

dec = 11
while not(kai(dec) and kai(bin(dec)[2:]) and kai(oct(dec)[2:])):
    dec += 2 #奇数だけ探せばよい(本文参照)
print(dec)

補足

n進数

2進数 0bNN
8進数 0oNN
16進数 0xNN

2進数に変換 bin(NN)
8進数に変換 oct(NN)
16進数に変換 hex(NN)

2進数を10進数に変換 int(BIN, 2)
8進数を10進数に変換 int(OCT, 2)
16進数を10進数に変換 int(HEX, 2)

文字列を逆にする

str[::-1]

参考 https://redcat-prog.hatenadiary.org/entry/20111104/1320395840

1
1
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
1
1