Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript Sum of all the factors of a number

<script>
    // Find sum of all divisors
// of a natural number
 
// Function to calculate sum of all
//divisors of a given number
function divSum(n)
{
    if(n == 1)
      return 1;
 
    // Sum of divisors
    let result = 0;
 
    // find all divisors
    // which divides 'num'
    for ( let i = 2; i <= Math.sqrt(n); i++)
    {
        // if 'i' is divisor of 'n'
        if (n % i == 0)
        {
            // if both divisors are same
            // then add it once else add
            // both
            if (i == (n / i))
                result += i;
            else
                result += (i + n / i);
        }
    }
 
    // Add 1 and n to result as
    // above loop considers proper
    // divisors greater than 1.
    return (result + n + 1);
}
 
// Driver Code
let n = 30;
document.write(divSum(n));
 
// This code is contributed by _saurabh_jaiswal.
</script>
Comment

PREVIOUS NEXT
Code Example
Javascript :: yarn create react app in current directory 
Javascript :: jquery append method 
Javascript :: graphql buildschema 
Javascript :: sequelize update index column 
Javascript :: js backtick new line 
Javascript :: javascript access pushed element 
Javascript :: java script append element to array 
Javascript :: js remove english word from string 
Javascript :: react-with-firebase-auth 
Javascript :: sequelize include stop returning the join table 
Javascript :: create auto increment mongodb mongoose 
Javascript :: push object into array javascript 
Javascript :: event delegation javascript 
Javascript :: classic asp json multidemsion json 
Javascript :: angular-chart.js 
Javascript :: how to get today in moment js 
Javascript :: how to adjust brightness with a slider in javascript 
Javascript :: use the whatwg url api instead 
Javascript :: cogo toast 
Javascript :: js promisify function 
Javascript :: lite youtube embed react 
Javascript :: javascript dom manipulation 
Javascript :: vscode format - .prettierrc jsx singleQuote not work 
Javascript :: javascript foreach object 
Javascript :: jest Cross origin http://localhost forbidden 
Javascript :: partial filter expression mongodb compass 
Javascript :: reverse array in js 
Javascript :: remove elements from map javascript 
Javascript :: connect node with react 
Javascript :: string to svg react 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =