import math
def gcd(a):
for i in range (1,a+1):
b=math.gcd(i,a)
b+=0
print(b)
a=int(input("Enter: "))
gcd(a)
**”’This is my code to find GCD of a given number ie.
eg,
1] if a=2 then gcd of (1,2)+ gcd(2,2) = 1+2=3
2] if a=3 then gcd(1,3)+(2,3)+(3,3) = 1+1+3=5
but i am unable to store value in b i.e" b+=0 " it doesent store plz help i want to get sum of all b as answer
How to do that??
(i am begineer in coding)”’**
>Solution :
You actually seem to want to get the sum of greatest common divisors of each number up to the given number including itself against the given number. You first want to define b, then in the loop add to its value, and at the end print the value (tho usually return would be used (return b) and then print(gcd(a))):
import math
def gcd(num):
b = 0
for i in range(1, num + 1):
b += math.gcd(i, num)
print(b)
a = int(input("Enter: "))
gcd(a)