def sum_divisors(n): sum = 0 # Return the sum of all divisors of n, not including n return sum print(sum_divisors(0)) # 0 print(sum_divisors(3)) # Should sum of 1 # 1 print(sum_divisors(36)) # Should sum of 1+2+3+4+6+9+12+18 # 55 print(sum_divisors(102)) # Should be sum of 2+3+6+17+34+51 # 114
I need help to solve this task using while statement
I need help to solve this task using while statement
|
Mar-11-2020, 03:26 PM
What have you tried? We'd be happy to help, but we need to see some effort for you (we're not going to do your homework for you).
Mar-11-2020, 06:45 PM
First question: how do you find the divisors of a number? Hint: do you know the modulo operator?
Try using the following steps.
Within the function declare a variable k and check the condition within the while loop. k=1 while(k<(n-1)): .....step1 .....step2 k+=1 STEP1:Using modulo operator check if the variable k is a multiple of n. STEP2:Increment the value of k every time to another variable(sum) if the above condition is True.
Mar-18-2020, 11:12 AM
(This post was last modified: Mar-18-2020, 11:14 AM by pyzyx3qwerty.)
first, you need to find the divisors of the number by using the modulo operator
use a for loop to make sure that divisor is less than the number then inside the for loop make a while loop for every number less than the original one, and is divisible by it inside the while loop, then print a statement to find the sum of all divisors
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela Need help on the forum? Visit help @ python forum For learning more and more about python, visit Python docs
try this
def sum_divisors(n): sum=1 x=int(n**0.5) for i in range(2,(x//1)+1): if n%i==0: sum=sum+i if n%i==0 and n/i!=i: sum=sum+(n/i) return int(sum)
Apr-02-2020, 11:34 AM
(Apr-01-2020, 08:38 PM)Jaikanth Wrote: try thisThis is a homework problem, you aren't supposed to write code for others
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela Need help on the forum? Visit help @ python forum For learning more and more about python, visit Python docs |
|
Possibly Related Threads… | |||||
Thread | Author | Replies | Views | Last Post | |
Stuck with using lists to solve task | gery576 | 9 | 2,846 |
Jan-16-2024, 06:29 PM Last Post: DPaul |
|
How to solve this task? | DERO | 2 | 2,560 |
Dec-06-2021, 07:15 PM Last Post: BashBedlam |
Users browsing this thread: 1 Guest(s)