Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php Sum of all the factors of a number

<?php
// 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
    $result = 0;
 
    // find all divisors
    // which divides 'num'
    for ( $i = 2; $i <= 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
$n = 30;
echo divSum($n);
 
// This code is contributed by SanjuTomar.
?>
Comment

PREVIOUS NEXT
Code Example
Php :: assertequals vs assertsame 
Php :: nested attributes - PHP 8.1 
Php :: phpstormda php faylning tepasiga avto kommet yozish 
Php :: dir_instalación_Zend_Studiopluginscom.zend.php.debug.debugger.win32.x86_10.6.0.v20140121-1240 esourcesphp.ini 
Php :: rollback a specific migration laravel 
Php :: undefined function bcmul php linux 
Php :: laravel firstorcreate with multiple parameters 
Php :: auto complete order paid2 
Php :: keep track of view count php 
Php :: Laravel - foreach on collection 
Php :: laravel 8 app with more than one database 
Php :: Max() Value And Min() Value 
Php :: laravel retain old value 
Php :: eager loading set limit to relationship 
Php :: CURLAUTH_BEARER cannot find 
Php :: how to stop a query if query after it is not inserted in laravel 
Php :: Anzeige von Custom Post Types in den Kategorien und Tags 
Php :: set count down CLI php 
Php :: magento2 join table with prefix 
Php :: generate random color php 
Php :: symfony 6 download 64 bit 
Php :: search php array 
Php :: php get result sql server 
Php :: woocommerce check if shop page 
Php :: file_get_contents with url 
Php :: laravel share 
Php :: progress bar calculate percentage php 
Php :: db($twoRandomPhotosOfSomePeoples); 
Java :: Java JDK 11 ubuntu 
Java :: priority queue reverse order java 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =