Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

i am not able to store the output in variable Help plz

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading