给定一个自然数,计算其所有适当因子之和。自然数的适当除数是严格小于该数的除数。 例如 ,数字20有5个适当的除数:1,2,4,5,10,除数之和为:1+2+4+5+10=22。 例如:
null
Input : num = 10Output: 8// proper divisors 1 + 2 + 5 = 8 Input : num = 36Output: 55// proper divisors 1 + 2 + 3 + 4 + 6 + 9 + 12 + 18 = 55
这个问题有着非常重要的意义 简单解决方案 ,我们都知道,对于任何一个数’num’,它的所有除数总是小于等于’num/2’,所有素因子总是小于等于 sqrt(数量) .所以我们迭代’i’直到i<=sqrt(num),对于任何’i’如果它除以’num’,那么我们得到两个除数’i’和’num/i’,不断地添加这些除数,但是对于一些数字,除数’i’和’num/i’在这种情况下只需添加一个除数,例如;num=36,所以对于i=6,我们将得到(num/i)=6,这就是为什么我们在求和中只得到一次6。最后我们加一,因为一是所有自然数的除数。
C++
// C++ program to find sum of all divisors of // a natural number #include<bits/stdc++.h> using namespace std; // Function to calculate sum of all proper divisors // num --> given natural number int divSum( int num) { // Final result of summation of divisors int result = 0; if (num == 1) // there will be no proper divisor return result; // find all divisors which divides 'num' for ( int i=2; i<= sqrt (num); i++) { // if 'i' is divisor of 'num' if (num%i==0) { // if both divisors are same then add // it only once else add both if (i==(num/i)) result += i; else result += (i + num/i); } } // Add 1 to the result as 1 is also a divisor return (result + 1); } // Driver program to run the case int main() { int num = 36; cout << divSum(num); return 0; } |
JAVA
// JAVA program to find sum of all divisors // of a natural number import java.math.*; class GFG { // Function to calculate sum of all proper // divisors num --> given natural number static int divSum( int num) { // Final result of summation of divisors int result = 0 ; // find all divisors which divides 'num' for ( int i = 2 ; i <= Math.sqrt(num); i++) { // if 'i' is divisor of 'num' if (num % i == 0 ) { // if both divisors are same then // add it only once else add both if (i == (num / i)) result += i; else result += (i + num / i); } } // Add 1 to the result as 1 is also // a divisor return (result + 1 ); } // Driver program to run the case public static void main(String[] args) { int num = 36 ; System.out.println(divSum(num)); } } /*This code is contributed by Nikita Tiwari*/ |
Python3
# PYTHON program to find sum of all # divisors of a natural number import math # Function to calculate sum of all proper # divisors num --> given natural number def divSum(num) : # Final result of summation of divisors result = 0 # find all divisors which divides 'num' i = 2 while i< = (math.sqrt(num)) : # if 'i' is divisor of 'num' if (num % i = = 0 ) : # if both divisors are same then # add it only once else add both if (i = = (num / i)) : result = result + i; else : result = result + (i + num / i); i = i + 1 # Add 1 to the result as 1 is also # a divisor return (result + 1 ); # Driver program to run the case num = 36 print (divSum(num)) # This code is contributed by Nikita Tiwari |
C#
// C# program to find sum of all // divisorsof a natural number using System; class GFG { // Function to calculate sum of all proper // divisors num --> given natural number static int divSum( int num) { // Final result of summation of divisors int result = 0; // find all divisors which divides 'num' for ( int i = 2; i <= Math.Sqrt(num); i++) { // if 'i' is divisor of 'num' if (num % i == 0) { // if both divisors are same then // add it only once else add both if (i == (num / i)) result += i; else result += (i + num / i); } } // Add 1 to the result as 1 // is also a divisor return (result + 1); } // Driver Code public static void Main() { int num = 36; Console.Write(divSum(num)); } } // This code is contributed by Nitin Mittal. |
PHP
<?php // PHP program to find sum of // all divisors of a natural number // Function to calculate sum of // all proper divisors // num --> given natural number function divSum( $num ) { // Final result of // summation of divisors $result = 0; // find all divisors // which divides 'num' for ( $i = 2; $i <= sqrt( $num ); $i ++) { // if 'i' is divisor of 'num' if ( $num % $i == 0) { // if both divisors are // same then add it only // once else add both if ( $i == ( $num / $i )) $result += $i ; else $result += ( $i + $num / $i ); } } // Add 1 to the result as // 1 is also a divisor return ( $result + 1); } // Driver Code $num = 36; echo (divSum( $num )); // This code is contributed by Ajit. ?> |
Javascript
<script> // Javascript program to find sum of all divisors of // a natural number // Function to calculate sum of all proper divisors // num --> given natural number function divSum(num) { // Final result of summation of divisors let result = 0; // find all divisors which divides 'num' for (let i=2; i<=Math.sqrt(num); i++) { // if 'i' is divisor of 'num' if (num%i==0) { // if both divisors are same then add // it only once else add both if (i==(num/i)) result += i; else result += (i + num/i); } } // Add 1 to the result as 1 is also a divisor return (result + 1); } // Driver program to run the case let num = 36; document.write(divSum(num)); // This code is contributed by Mayank Tyagi </script> |
输出:
55
请参考下面的帖子,了解优化的解决方案和配方。 一个数的所有因子之和的有效解 本文由 沙申克·米什拉(古卢) .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END