开发者

euler project sum of digits’ factorials

开发者 https://www.devze.com 2023-01-05 11:43 出处:网络
here is problem about sum of factorial of digit http://projecteuler.net/index.php?section=problems&id=254

here is problem about sum of factorial of digit

http://projecteuler.net/index.php?section=problems&id=254

also here is Define sf(n) as the sum of the digits of f(n). So sf(342) = 3 + 2 = 5.

Here is code which prints 32 as sum of digit's factorials but it does not show me second result

import java.util.*;
public class fact_of_digit1 {
public static int fact(int a){
    if (a==0)  return 1;
     return a*fact(a-1);
}
   public static int factorialofdigit(int a){
       int sum=0;
       int t=0;
       while (a!=0){
           t= a%10;
            a/=10;
            sum+=fact(t);
       }
       return sum;

   }
    public static void main(String[] args) {
       Scanner scnr=new Scanner (System.in);
       System.out.println("enter a:");
       int a=scnr.nextInt();
     System.out.println(factorialofdigit(a));
     System.out.println(sfn(a));
    }
    public static int sfn(i开发者_如何学JAVAnt a){
        int sum1=0;
        int t=factorialofdigit(a);
         while (t!=0){
             sum1+=t%10;
             t/=10;



         }
         return sum1;
    }

}

result is:

enter a:
342
32
BUILD SUCCESSFUL (total time: 3 seconds)


The code looks fine, and works here, so it may be something to do with the IDE or how you're using it.

Are you running it debug mode? Did you set any breakpoints?

What happens if you create a new project and copy the code to it?

Does the following program work?

import java.util.*;
public class fact_of_digit1 {
    public static void main(String[] args) {
        Scanner scnr=new Scanner (System.in);
        System.out.println("enter a:");
        int a=scnr.nextInt();
        System.out.println(5);
        System.out.println(3);
    }
}

Replace the scanner with "int a = 342".

Add an extra println statement at the end.


The main function has two print statements which aren't in a loop. You will only get those two numbers printed.


Check out this one we need to calculate summation of sg(i)

import java.util.*;
public class FactOfDigits {
public static int fact(int a){
    if (a==0)  return 1;
     return a*fact(a-1);
}
   public static int factorialofdigit(int a){
       int sum=0;
       int t=0;
       while (a!=0){
           t= a%10;
            a/=10;
            sum+=fact(t);
       }
       return sum;

   }
    public static void main(String[] args) {
       Scanner scnr=new Scanner (System.in);
       System.out.println("enter a:");   
       int in=20;
       int sum=0;
       for(int i=1;i<=in;i++) {
           sum+=sgfn(i); // summing sg(i)
       }
     System.out.println(sum%1000000); 
    }
    public static int sfn(int a){
        int sum1=0;
        int t=factorialofdigit(a);
         while (t!=0){
             sum1+=t%10;
             t/=10;

         }
         return sum1;
    }
    public static int gfn(int num) {
        int answer =0;
        for(int i=1;i<Integer.MAX_VALUE;i++) {
            if(num == sfn(i)) {
                return i;
            }
        }
        
        return answer;
    }
    
    public static int sgfn(int num) {
        int answer =0;
        int gfn = gfn(num);
         while (gfn!=0){
             answer+=gfn%10;
             gfn/=10;
         }
        
        return answer;
    }

}


def f(n):
    from math import factorial
    lst_n = list(map(int, str(n).strip()))
    s=0
    for i in lst_n:
        s += factorial(i)
    return s
def sf(n):
    return sum(list(map(int, str(f(n)))))

def g(i):
    for n in range(1, 100000000):
        if sf(n)==i:
            break
    return n
def sg(n):
    s= 0
    for i in range(1, n+1):
        s += sum(list(map(int, str(g(i)))))
    return s
N= int(input())
LST=[]
for i in range(N):
    LST.append(list(map(int, input().strip().split())))
for i in range(N):
    print(sg(LST[i][0]))
0

精彩评论

暂无评论...
验证码 换一张
取 消