Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

find sum of factors of a number python

def sumFactors(n):
    return sum(d for d in range(1, n) if n%d == 0)
Comment

python Sum of all the factors of a number

# Simple Python 3 program to
# find sum of all divisors of
# a natural number
import math 
 
# Function to calculate sum
# of all divisors of given
#  natural number
def divSum(n) :
    if(n == 1):
       return 1
 
    # Final result of summation
    # of divisors
    result = 0
   
    # find all divisors which
    # divides 'num'
    for i in range(2,(int)(math.sqrt(n))+1) :
 
        # if 'i' is divisor of 'n'
        if (n % i == 0) :
 
            # if both divisors are same
            # then add it only once
            # else add both
            if (i == (n/i)) :
                result = result + i
            else :
                result = result + (i + n//i)
         
         
    # Add 1 and n to result as above
    # loop considers proper divisors
    # greater than 1.
    return (result + n + 1)
   
# Driver program to run the case
n = 30
print(divSum(n))
 
# This code is contributed by Nikita Tiwari.
Comment

PREVIOUS NEXT
Code Example
Python :: string to array python 
Python :: nested loop in list comprehension 
Python :: tkinter disable button styles 
Python :: how to run terminal commands in python 
Python :: oython 
Python :: how to remove the last letter of a string python 
Python :: python substring count 
Python :: pytube sample script 
Python :: py factors of a number 
Python :: pytorch transpose 
Python :: beautifulsoup find get value 
Python :: numpy int64 to int 
Python :: max in a list python 
Python :: image crop in python 
Python :: how to use argparse 
Python :: python check if list contains 
Python :: create columns in streamlit 
Python :: python recursively merge dictionaries 
Python :: django slug int url mapping 
Python :: import python script from another directory 
Python :: python print string name in pattern 
Python :: custom position for axis matplotlib 
Python :: how to fix valueerror in python 
Python :: python array colon 
Python :: string remove in python 
Python :: python package for misspelled words 
Python :: drop rows where specific column has null values 
Python :: python os get path 
Python :: python last item in list 
Python :: python variable 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =