如果一个数的每一位的阶乘之和等于该数本身,则称该数为彼得森数。
null
例子:
Input : n = 145Output = YesExplanation: 145 = 5! + 4! + 1! = 120 + 24 +1 = 145Input : n = 55Output : NoExplanation: 5! + 5! = 120 + 120 = 240Since 55 is not equal to 240It is not a Peterson number.
我们将选取给定数字的每个数字(从最后一个数字开始),并找到其阶乘。加上所有的阶乘。最后,我们检查阶乘和是否等于数字。
C++
// C++ program to determine whether the number is // Peterson number or not #include <iostream> using namespace std; // To quickly find factorial of digits int fact[10] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880 }; // Function to check if a number is Peterson // or not bool peterson( int n) { int num = n, sum = 0; // stores the sum of factorials of // each digit of the number. while (n > 0) { int digit = n % 10; sum += fact[digit]; n = n / 10; } // Condition check for a number to // be a Peterson Number return (sum == num); } // Driver Program int main() { int n = 145; if (peterson(n)) cout << "Yes" ; else cout << "No" ; return 0; } |
JAVA
//checks whether a number entered by user is peterson number or not import java.util.*; class GFG { public static void main(String args[]) { Scanner sc= new Scanner(System.in); //taking input from the user System.out.println( "Enter the number" ); int num=sc.nextInt(); int temp=num; //storing the number in a temporary variable int f= 1 ,sum= 0 ; while (num!= 0 ) //running while loop until number becomes zero { f= 1 ; //extracting last digit of the number //and storing in r int r=num% 10 ; //for loop to find the factorial of a digit for ( int i= 1 ;i<=r;i++) { f=f*i; } sum=sum+f; //adding the factotial of the digits num=num/ 10 ; } //checking if the sum of the factorial of digits //is equal to the number or not if (sum==temp) System.out.println( "PETERSON NUMBER" ); else System.out.println( "NOT PETERSON NUMBER" ); } } |
Python3
# Python3 code to determine whether the # number is Peterson number or not # To quickly find factorial of digits fact = [ 1 , 1 , 2 , 6 , 24 , 120 , 720 , 5040 , 40320 , 362880 ] # Function to check if a number # is Peterson or not def peterson(n): num = n sum = 0 # stores the sum of factorials of # each digit of the number. while n > 0 : digit = int (n % 10 ) sum + = fact[digit] n = int (n / 10 ) # Condition check for a number # to be a Peterson Number return ( sum = = num) # Driver Code n = 145 print ( "Yes" if peterson(n) else "No" ) # This code is contributed by "Sharad_Bhardwaj".. |
C#
// C# program to determine whether the // number is Peterson number or not using System; public class GFG { // To quickly find factorial of digits static int [] fact = new int [10] { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880 }; // Function to check if a number is // Peterson or not static bool peterson( int n) { int num = n; int sum = 0; // stores the sum of factorials of // each digit of the number. while (n > 0) { int digit = n % 10; sum += fact[digit]; n = n / 10; } // Condition check for a number to // be a Peterson Number return (sum == num); } // Driver Program static public void Main() { int n = 145; if (peterson(n)) Console.WriteLine( "Yes" ); else Console.WriteLine( "No" ); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to determine // whether the number is // Peterson number or not // To quickly find // factorial of digits $fact = array (1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880); // Function to check if // a number is Peterson // or not function peterson( $n ) { $num = $n ; $sum = 0; // stores the sum of factorials of // each digit of the number. while ( $n > 0) { $digit = $n % 10; $n = $n / 10; } // Condition check for // a number to be a // Peterson Number return ( $sum == $num ); } // Driver Code $n = 145; if (peterson( $n )) echo "Yes" ; else echo "No" ; // This code is contributed by ajit ?> |
Javascript
<script> // Javascript program to determine whether // the number is Peterson number or not // To quickly find factorial of digits let fact = [ 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880 ]; // Function to check if a number is // Peterson or not function peterson(n) { let num = n, sum = 0; // stores the sum of factorials of // each digit of the number. while (n > 0) { let digit = n % 10; sum += fact[digit]; n = parseInt(n / 10); } // Condition check for a number to // be a Peterson Number return (sum == num); } // Driver code let n = 145; if (peterson(n)) document.write( "Yes" ); else document.write( "No" ); // This code is contributed by souravmahato348 </script> |
输出:
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END