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.

cevap : 31626

link

ipucu icin buraya bakabilirsiniz.

Derleme ortami pycharm python

main.py dosyasi:

import func

checked_list = []
for number in range (2,10001):
    sum = func.all_calculations(number)
    number2 = sum
    sum2 = func.all_calculations(number2)
    if (number == sum2) & (number != sum):
        checked_list.append(number)

result_sum = 0
for i in range(len(checked_list)):
    result_sum += checked_list[i]

print("##########################")
print("hesaplamalar tamam")
print("1-10000 arasindaki amicable sayilarin listesi : ", checked_list)
print("amicable sayilarin toplami : ", result_sum)

func.py dosyasi :

import collections # OrderedDict icin library - eleman_sayilarini_iliskilendir fonk'unun icinde

def prime_divisor_calc(number):
    i = 2
    list = []

    while (i <= number):
        if (number % i) == 0:
            division = number / i
            number = division
            list.append(i)
        else:
            i = i + 1
    return list

def number_of_list_items(list):
    list_items = []

    for i, item in enumerate(list):
        list_items.append(list.count(item))
    return list_items

def correlate_item_numbers(list_1, list_2):
    dict = collections.OrderedDict()
    for i in range(len(list_1)):
        dict[list_1[i]] = list_2[i]
    return dict

def sum_of_divisors(dict,number):
    product = 1
    for i in range(len(dict)):
        product = product * (pow(list(dict.keys())[i], list(dict.values())[i] + 1) - 1) / (list(dict.keys())[i] - 1)
    sum = product - number #project euler soru 21'de toplamda sayinin kendisinin dahil edilmesini istemiyor.
    return sum

def all_calculations(number):
    divisors_list = prime_divisor_calc(number)
    print("bolenler listesi:", divisors_list)

    list_items = number_of_list_items(divisors_list)

    dict = correlate_item_numbers(divisors_list, list_items)

    # hangi carpandan kac tane var ekrana bas
    for i in range(len(dict)):
        print("%s den %s tane var" % (list(dict.keys())[i], list(dict.values())[i]))

    sum = sum_of_divisors(dict, number)

    return sum