이경수 선생님의 수학실험실

Problem 23(Non-abundant sums) 본문

Project Euler

Problem 23(Non-abundant sums)

(이경수) 2019. 4. 16. 21:08

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.

 

In Python:

#PE23 Non-abundant sums
import time

startTime = time.time()
listSumAbdt = []
signSumAbdt = []
val = 28123

def isabdt(n):
    sum = 0
    for i in range(1, n):
        if n % i == 0:
            sum += i
    if n < sum:
        return True
    else:
        return False

listAbdt = [num for num in range(1, val + 1) if isabdt(num) is True]

for i in range(0, val + 1):
    signSumAbdt.append(0)

signSumAbdt[24] = 1

for i in range(1, 24):
    listSumAbdt.append(i)

for i in range(24, val + 1):
    if signSumAbdt[i] == 1:
        pass
    else:
        for j in listAbdt:
            if i - j < 0:
                listSumAbdt.append(i)
                break
            else:
                if i - j in listAbdt:
                    for k in range(1, int(val/i)):
                        signSumAbdt[i * k] = 1
                    break
print(listSumAbdt)
print(sum(listSumAbdt))
print(time.time() - startTime, "seconds")

Run time: 440.997004032135 seconds

 

In Python:

#PE23 Non-abundant sums
import time

startTime = time.time()
val = 28123
sum = 0
codeSum = []

def isabdt(n):
    sum = 0
    for i in range(1, int(n / 2) + 1):
        if n % i == 0:
            sum += i
    if n < sum:
        return True
    else:
        return False

listAbdt = [num for num in range(1, val + 1) if isabdt(num) is True]

for i in range(0, val + 1):
    codeSum.append(0)

for i in listAbdt:
    for j in listAbdt:
        if i + j <= val:
            codeSum[i + j] = 1

for i in range(1, val + 1):
    if codeSum[i] == 0:
        sum += i
print(sum)
print(time.time() - startTime, "seconds")

Run time: 25.786690950393677 seconds

 

In Python:

#PE23 Non-abundant sums
import time
import math

startTime = time.time()
val = 28123
result = 0
codeSum = []

def isabdt(n):
    listDiv = [1]
    for i in range(2, int(math.sqrt(n)) + 1):
        if n % i == 0:
            listDiv.append(i);
            listDiv.append(n // i);
    return sum(set(listDiv))

listAbdt = [num for num in range(1, val + 1) if isabdt(num) > num]

for i in range(0, val + 1):
    codeSum.append(0)
for i in listAbdt:
    for j in listAbdt:
        if i + j <= val:
            codeSum[i + j] = 1
for i in range(1, val + 1):
    if codeSum[i] == 0:
        result += i

print(result)
print(time.time() - startTime, "seconds")

Run time: 9.77357530593872 seconds

 

In Java:

//Euler23 Non-abundant sums
package project_euler21_30;

import java.util.ArrayList;

public class Euler23 {
	public static boolean isabdt(int n) {
		int sum = 0;
		for (int i = 1; i < Math.floor(n/2) + 1; i++) {
			if (n % i == 0) {
				sum += i;
			}
		}
		if (n < sum) {
			return true;
		} else {
			return false;
		}
	}
	public static void main(String[] arg) {
		long startTime = System.currentTimeMillis();
		int value = 28123;
		long sum = 0;
		ArrayList<Integer> listAbdt = new ArrayList<Integer>();
		ArrayList<Integer> codeSum = new ArrayList<Integer>();
		for (int i = 0; i < value + 1; i++) {
			if (isabdt(i) == true) {
				listAbdt.add(i);
			}
		}
		for (int i = 0; i < value + 1; i++) {
			codeSum.add(0);
		}
		for (int i = 0; i < listAbdt.size(); i++) {
			for (int j = 0; j < listAbdt.size(); j++) {
				if (listAbdt.get(i) + listAbdt.get(j) <= value) {
					codeSum.set(listAbdt.get(i) + listAbdt.get(j), 1);
				}
			}
		}
		for (int i = 0; i < codeSum.size(); i++) {
			if (codeSum.get(i) == 0) {
				sum += i;
			}
		}
		System.out.println(sum);
		long endTime = System.currentTimeMillis();
		System.out.println((double)(endTime - startTime)/(double)1000 + "seconds");
	}
}

Run time: 1.305seconds

 

Solution: 4179871

'Project Euler' 카테고리의 다른 글

Problem 25(1000-digit Fibonacci number)  (0) 2019.05.11
Problem 24(Lexicographic permutations)  (0) 2019.04.22
Problem 22(Names scores)  (0) 2019.04.14
Problem 21(Amicable numbers)  (0) 2019.04.14
Problem 20 (Factorial digit sum)  (0) 2019.04.07
Comments