mu chance or much chance ?

日々の戯れ言

身体性人工知能

身体性人工知能について勉強しようと思い,

以下の本を購入しました.

知能の原理 ―身体性に基づく構成論的アプローチ―

知能の原理 ―身体性に基づく構成論的アプローチ―

Project Euler 25

  • 問題

Problem 25:1000-digit Fibonacci number
The Fibonacci sequence is defined by the recurrence relation:

Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1.
Hence the first 12 terms will be:

F1 = 1
F2 = 1
F3 = 2
F4 = 3
F5 = 5
F6 = 8
F7 = 13
F8 = 21
F9 = 34
F10 = 55
F11 = 89
F12 = 144
The 12th term, F12, is the first term to contain three digits.

What is the index of the first term in the Fibonacci sequence to contain 1000 digits?

  • 解答例
f1 = 1
f2 = 1
i = 3
while len(str(f1 + f2)) < 1000:
    f1, f2 = f2, (f1 + f2)
    i += 1
    
print(i)

Project Euler 24

  • 問題

Problem 24:Lexicographic permutations
A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:

012 021 102 120 201 210

What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?

  • 解答例
import itertools

p = list(itertools.permutations(range(10)))

for i in p[999999]:
    print(i, end="")

Project Euler 23

  • 問題

Problem 23:Non-abundant sums
A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.

A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.

As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.

Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.

  • 解答例
def check2sum(n, a, f):
    for i in a:
        if i > n / 2:
            break
        if f[n - i]:
            return True
    return False

def sumDivisor(num):
    sum = 0
    i = 1
    while i * i < num:
        if num % i == 0:
            sum += i
            sum += (num // i)
        i += 1
    if i * i == num:
        sum += i
    return sum

array = []
count = []
flag = [False] * 28124

for i in range(1, 28124):
    if i < sumDivisor(i) - i:
        flag[i] = True
        array.append(i)

for i in range(1, 28124):
    if not check2sum(i, array, flag):
        count.append(i)

print(sum(count))

Project Euler 22

  • 問題

Problem 22:Names scores
Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.

For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.

What is the total of all the name scores in the file?

  • 解答例
num = { "A":1,
        "B":2,
        "C":3,
        "D":4,
        "E":5,
        "F":6,
        "G":7,
        "H":8,
        "I":9,
        "J":10,
        "K":11,
        "L":12,
        "M":13,
        "N":14,
        "O":15,
        "P":16,
        "Q":17,
        "R":18,
        "S":19,
        "T":20,
        "U":21,
        "V":22,
        "W":23,
        "X":24,
        "Y":25,
        "Z":26}

f = open("p022_names.txt", "r")

name = []
for line in f:
    text = line.replace('\n', '')
    text = text.replace('\r', '')
    text = text.replace('"', '')
    name = text.split(",")
f.close()
name.sort()

l = 0
count = 0
for n in name:
    l += 1
    temp = 0
    for i in range(len(n)):
        temp += num[n[i]]
    count += (l * temp)

print(count)
  • 用意したファイル(p022_names.txt)

https://projecteuler.net/project/resources/p022_names.txt

Project Euler 21

  • 問題

Problem 21:Amicable numbers
Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).
If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers.

For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.

Evaluate the sum of all the amicable numbers under 10000.

  • 解答例
def sumDivisor(num):
    sum = 0
    i = 1
    while i * i < num:
        if num % i == 0:
            sum += i
            sum += (num // i)
        i += 1
    if i * i == num:
        sum += i
    return sum

sum = 0
for i in range(1, 10000):
    temp = sumDivisor(i)
    temp2 = sumDivisor(temp - i)
    if i == temp2 - (temp - i) and not(i == temp - i):
        sum += i

print(sum)