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 :: true and false in python 
Python :: how to iterate through a list of numbers in python 
Python :: python coding practice 
Python :: uninstall python ubuntu 18.04 
Python :: read variable in a string python 
Python :: how to exit a loop in python 
Python :: how to put space in between list item in python 
Python :: python requests insecure request warning 
Python :: pahtlib join path 
Python :: how to print a value of a key in nested dictionary python 
Python :: remove last digit from number python 
Python :: count items in list python by loop 
Python :: index start from 1 pandas 
Python :: normalizer in sklearn 
Python :: how to delete a column in pandas dataframe 
Python :: gcd python 
Python :: django form 
Python :: how to save plot in matplotlib 
Python :: reversing in python 
Python :: NumPy roll Syntax 
Python :: Python script to SSH to server and run command 
Python :: sqlite query using string as parameter in python 
Python :: literal_eval in python 
Python :: discord.py create button 
Python :: spliting the text to lines and keep the deliminaters python 
Python :: delete content of table django 
Python :: recursion in python 
Python :: remove python 2.7 centos 7 
Python :: clear many to many django 
Python :: Python Switch case statement by Dictionary Mapping 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =