aboutsummaryrefslogtreecommitdiff
path: root/challenge-089/paulo-custodio/python/ch-1.py
blob: 7b5cc6d4f9e932514c05f69b6dc42a8ee158fee4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env python3

# Challenge 089

# TASK #1 > GCD Sum
# Submitted by: Mohammad S Anwar
# You are given a positive integer $N.
#
# Write a script to sum GCD of all possible unique pairs between 1 and $N.

# This solution uses a recursive algorithm to compute the GCD.

import sys

def gcd(a, b):
    if a==0:
        return b
    else:
        return gcd(b%a, a)

def sum_gcd(n):
    sum = 0
    for a in range(1, n):
        for b in range(a+1, n+1):
            sum += gcd(a, b)
    return sum

print(sum_gcd(int(sys.argv[1])))