以下サイトで挑戦できる。
https://projecteuler.net/
最初の10問をPythonでやってみた。
Problem 1
In [28]: sum([x for x in range(1000) if x%5==0 or x%3==0]) Out[28]: 233168
Problem 2
In [32]: while fibList[-1] < 4000000: ...: fibList.append(fibList[-1]+fibList[-2]) In [34]: sum([x for x in fibList if x&1==0]) Out[34]: 4613732
Problem 3
In [52]: n = 600851475143 ...: lst = [] ...: i = 3 ...: while n >= i: ...: if n%i==0: ...: lst.append(i) ...: n/=i ...: else: ...: i+=1 ...: max(lst) Out[52]: 6857
Problem 4
In [53]: lst = [] ...: for i in range(100,1000,1): ...: for j in range(100,1000,1): ...: n = str(i*j) ...: if n == "".join(list(reversed(n))): ...: lst.append(int(n)) ...: max(lst) Out[53]: 906609
Problem 5
In [57]: 2*2*2*2*3*3*5*7*11*13*17*19 Out[57]: 232792560
Problem 6
In [59]: sum([x for x in range(101)])**2 - sum([x**2 for x in range(101)]) Out[59]: 25164150
Problem 7
In [60]: max(PrimeNumList(10001)) Out[60]: 104743素数のリストを求めるコードは前の投稿参照。
Problem 8
In [75]: num="731671765313306249192251196744265747423553491949349698352031277450632623957831801698480186947885184385861 ...: 56078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557668966489504452 ...: 44523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077 ...: 23907138105158593079608667017242712188399879790879227492190169972088809377665727333001053367881220235421809751 ...: 25454059475224352584907711670556013604839586446706324415722155397536978179778461740649551492908625693219784686 ...: 22482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427 ...: 17147992444292823086346567481391912316282458617866458359124566529476545682848912883142607690042242190226710556 ...: 26321111109370544217506941658960408071984038509624554443629812309878799272442849091888458015616609791913387549 ...: 92005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257 ...: 530420752963450" In [76]: maxNum = 0 ...: for i in range(len(num) - 12): ...: prd = eval("*".join(num[i:i+13])) ...: maxNum = max([prd, maxNum]) ...: maxNum Out[76]: 23514624000
Problem 9
In [80]: for a in range(1,1000,1): ...: for b in range(1,1000,1): ...: c = 1000 - a - b ...: if a**2 + b**2 == c**2: ...: print(a, b, c) ...: print(a*b*c) ...: break ...: else: ...: continue ...: break ...: 200 375 425 31875000
Problem 10
In [81]: s=0 ...: for i in range(2000000): ...: if isPrime(i): ...: s+=i ...: s Out[81]: 142913828922
0 件のコメント:
コメントを投稿