Given a starting number and the ending number the task is to find the all the Peterson Number present in that given range and also to display it’s frequency.
Peterson Number is a number whose sum of factorial to it’s digits are equal to the original number.
Example 1: 145. 1!+4!+5! = 1+24+120 = 145. Hence it is a Peterson Number.
Example 2: 415. 4!+1!+5! = 24+1+120 = 145. Here the result is not equal to the original number. Hence it is not a Peterson Number.
Java
import java.util.*;
public class PetersonNumberRange
{
public static void main(String args[])
{
int start=0,end=0,fact=1,frequency=0,n=0,s=0,r=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a starting range: ");
start=sc.nextInt();
System.out.print("Enter a ending range: ");
end=sc.nextInt();
System.out.println("Peterson Numbers in the given range are:-");
for(int i=start;i<=end;i++)
{
n=i;
while(n>0)
{
r=n%10;
while(r>0)
{
fact=fact*r;
r--;
}
s=s+fact;
fact=1;
n=n/10;
}
if(s==i)
{
System.out.println(i);
frequency++;
}
s=0;
}
System.out.println("Frequency: "+frequency);
}
}Java