Project Euler – Problem # 21 – Solved with Go

Problem:

Evaluate the sum of all amicable pairs under 10000.

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.

One Possible Solution: Go

package main

import "fmt"

func main() {
	fmt.Println("Hello, playground")
	tally := 0
	for y := 1; y <= 10000; y++ {
		x := factors(y)
		x1 := get_total(x)
		x3 := factors(x1)
		x4 := get_total(x3)
		if y == x4 && y != x1 {
			tally += y
		}
	}
	fmt.Println(tally)
}

func get_total(factors []int) (number int) {
	total := 0
	for i := 0; i < len(factors); i++ {
		total += factors[i]
	}
	return total
}

func factors(number int) (factors []int) {
	L := make([]int, number)
	counter := 0
	for i := 1; i < ((number / 2) + 1); i++ {
		if number%i == 0 {
			L[counter] = i
			counter += 1
		}
	}
	return L[:counter]

}
Advertisements

Project Euler – Problem # 21 – Solved with Java

Problem:

Evaluate the sum of all amicable pairs under 10000.

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.

One Possible Solution: Java

import java.util.ArrayList;
import java.util.List;

public class Problem_21 {
	public static void main(String args[]){
		int tally = 0;
		for(int y = 1; y <= 10000; y ++){
			List<Integer> x = factors(y);
			int x1 = get_total(x);
			List<Integer> x3 = factors(x1);
			int x4 = get_total(x3);
			if((y == x4) && (y != x1)){
				tally += y;
			}
		}
		System.out.format("Tally = %d%n", tally);
	}
	
	public static List<Integer> factors(int number){
		List<Integer> L = new ArrayList<Integer>();
		for(int i = 1; i <= ((number / 2) + 1); i ++){
			if(number % i == 0){
				L.add(i);
			}
		}
		return L;
	}
	
	public static Integer get_total(List<Integer> list) {
		     Integer sum= 0; 
		     for (Integer i:list)
		         sum = sum + i;
		     return sum;
	}
}

Project Euler – Problem # 21 – Solved with Python

Problem:

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.

One Possible Solution:

def factor(number):
    """Factor a numer"""
    L = []
    for i in range(1, ((number / 2) + 1)):
        if number%i == 0:
            L.append(i)
    return L

def get_total(x):
    """Total the elements in list L"""
    total = sum(x)
    return total

def main():
    """Main program"""
    tally = 0 
    for y in range(1, 10001):
        x = factor(y) # A list
        x1 = get_total(x)
        x3 = factor(x1) # A list
        x4 = get_total(x3)
        if (y == x4) and (y != x1):
            tally = tally + y
            print "tally = %s y = %s" % (tally, y)
    print "Final Tally = %s" % tally
            
if __name__ == '__main__':
    main()