正整数的数字根是通过对整数的位数求和得到的。如果结果值是一个数字,则该数字是数字根。如果结果值包含两个或两个以上的数字,则将这些数字相加,并重复该过程。只要有必要获得一个位数,这一过程就会持续。 给定一个数字,任务是找到它的数字根。输入的数字可能很大,即使使用long int,也可能无法存储。 请进来 ACM-ICPC 例如:
Input : num = "1234"Output : 1Explanation : The sum of 1+2+3+4 = 10, digSum(x) == 10 Hence ans will be 1+0 = 1Input : num = "5674"Output : 4
我们已经在下面的帖子中讨论了一个数字的解决方案。 求一个数的位数之和,直到和变成一位数 在这篇文章中,类似的方法被大量讨论。
方法1
查找65785412的数字根 步骤:
- 找出一个数字的所有数字
- 把所有的数字一个接一个地加起来
- 如果最后的总数是两位数,再加一次,使之成为一位数
- 以一位数获得的结果是数字的数字根
例子: 输入: 65785412 查找数字根:(6+5+7+8+5+4+1+2)=38=>11=>(1+1)=2 输出: 2.
方法2
这个想法基于这样一个事实:对于非零数字num,如果数字可被9整除,则数字根为9,否则数字根为num%9。(请看 http://www.sjsu.edu/faculty/watkins/Digitsum0.htm (详情请参阅) 查找65785412的数字根 步骤:
- 数字之和=6+5+7+8+5+4+1+2=38
- 因为38不是9的倍数,所以数字根是38%9=2。
C++
// C++ program to find digital root of a number #include<bits/stdc++.h> using namespace std; // Returns digital root of num int digitalRoot(string num) { // If num is 0. if (num.compare( "0" ) == 0) return 0; // Count sum of digits under mod 9 int ans = 0; for ( int i=0; i<num.length(); i++) ans = (ans + num[i]- '0' ) % 9; // If digit sum is multiple of 9, answer // 9, else remainder with 9. return (ans == 0)? 9 : ans % 9; } // Driver code int main() { string num = "65785412" ; // Calling digitalRoot function cout<< digitalRoot(num) <<endl; return 0; } // Note: Special case when num = "00..." // program will not give correct output. |
JAVA
// Java code for digital root import java.util.*; public class GfG { static int digroot( int n) { int root = 0 ; // Loop to do sum while // sum is not less than // or equal to 9 while (n > 0 || root > 9 ) { if (n == 0 ) { n = root; root = 0 ; } root += n % 10 ; n /= 10 ; } return root; } // Driver code public static void main(String argc[]) { int n = 65785412 ; System.out.println(digroot(n)); } } // This code is contributed by Gitanjali. // This code will run for 0000 testcase also. |
Python3
# Python3 program to find digital root # of a number import math # Returns digital root of num def digitalRoot(num): # If num is 0. if (num = = "0" ): return 0 # Count sum of digits under mod 9 ans = 0 for i in range ( 0 , len (num)): ans = (ans + int (num[i])) % 9 # If digit sum is multiple of 9, answer # 9, else remainder with 9. if (ans = = 0 ): return 9 else : return ans % 9 # Driver method num = "65785412" # Calling digitalRoot function print (digitalRoot(num)) # This code is contributed by Gitanjali. |
C#
// C# code for digital root using System; class GfG { static int digroot( int n) { int root = 0; // Loop to do sum while // sum is not less than // or equal to 9 while (n > 0 || root > 9) { if (n == 0) { n = root; root = 0; } root += n % 10; n /= 10; } return root; } // Driver code public static void Main() { int n = 65785412; Console.Write(digroot(n)); } } // This code is contributed by Smitha // This code will run for 0000 testcase also. |
PHP
<?php // PHP program to find // digital root of a number // Returns digital root of num function digroot( $n ) { $root = 0; // Loop to do sum while // sum is not less than // or equal to 9 while ( $n > 0 || $root > 9) { if ( $n == 0) { $n = $root ; $root = 0; } $root += $n % 10; $n /= 10; } return $root ; } // Driver code $num = 65785412; // Calling digitalRoot function echo digroot( $num ); // This code is contributed by Sam007 ?> |
Javascript
<script> // Javascript code for digital root function digroot(n) { let root = 0; // Loop to do sum while // sum is not less than // or equal to 9 while (n > 0 || root > 9) { if (n == 0) { n = root; root = 0; } root += n % 10; n = parseInt(n / 10, 10); } return root; } let n = 65785412; document.write(digroot(n)); </script> |
输出:
2
本文由 希夫·普拉塔普·辛格 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。