34-Digit factorials

Digit factorials

Problem 34

145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.

Find the sum of all numbers which are equal to the sum of the factorial of their digits.

Note: as 1! = 1 and 2! = 2 are not sums they are not included.

package main

import (
   "fmt"
   "strconv"
)

/*
Digit factorialsProblem 34
145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.
Find the sum of all numbers which are equal to the sum of the factorial of their digits.
Note: as 1! = 1 and 2! = 2 are not sums they are not included.
*/
/**
1.把数字转化成字符串,然后拆开
2.判断是否相等
*/
//求一个数的阶乘的函数
func Factorial(n int) int {
   var result int
   if n > 0 {
      result = n * Factorial(n-1)
      return result
   }
   return 1
}
func Breakjc(i int) int {
   result := 0
   str := strconv.Itoa(i)
   for _, n := range str {
      result += Factorial(int(n - 48))
   }
   return result
}
func main() {
   var i, j int
   i = 3
   //求所有组成数字的阶乘和的函数
   for {
      j = Breakjc(i)
      if i == j {
         fmt.Println(i)
      }
      i++
   }

}
 

结果:40730

原文地址:https://www.cnblogs.com/miria-486/p/10153164.html

时间: 2024-10-05 04:56:57

34-Digit factorials的相关文章

Project Euler:Problem 34 Digit factorials

145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. Find the sum of all numbers which are equal to the sum of the factorial of their digits. Note: as 1! = 1 and 2! = 2 are not sums they are not included. #include <iostream> #include <v

Project-Euler problem 1-50

最近闲的做了下Project Euler 上的题目,前面50题都比较简单,简单总结下.代码一般是Python和C/C++的 用Python 做这些题目简直是酸爽啊 一下代码可能不一定是我的,因为不知道论坛里面的回复不是永久的,所以我的代码有的丢了,可能找个和我的意思相近的代码.题目翻译是从 欧拉计划 | Project Euler 中文翻译站上面Copy 的表告我. Problem 1  Multiples of 3 and 5 10以下的自然数中,属于3和5的倍数的有3,5,6和9,它们之和是

EularProject 34: 一个数字与他每位数的阶乘和

Digit factorials Problem 34 145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. Find the sum of all numbers which are equal to the sum of the factorial of their digits. Note: as 1! = 1 and 2! = 2 are not sums they are not included. Answer:

动手动脑,无法自拔(3)课时作业6

1.动手动脑(五子棋棋盘排布) (1)源程序 1 import java.io.*; 2 3 public class QiPan 4 { 5 //定义一个二维数组来充当棋盘 6 private String[][] board; 7 //定义棋盘的大小(宏定义) 8 private static int BOARD_SIZE = 15; 9 10 //初始化棋盘 11 public void initBoard() 12 { 13 //初始化棋盘数组 14 board = new String

The US ASCII Character Set 对应码 可以解决 URL中的特殊符号的传输问题

The US ASCII Character Set US ASCII, ANSI X3.4-1986 (ISO 646 International Reference Version) Codes 0 through 31 and 127 (decimal) are unprintable control characters.Code 32 (decimal) is a nonprinting spacing character. Codes 33 through 126 (decimal)

Java面试题-1

Java面试题 Java面试题 1 1.面向对象的特征有哪些方面? 6 2.访问修饰符public,private,protected,以及不写(默认)时的区别? 7 3.String 是最基本的数据类型吗? 8 4.float f=3.4;是否正确? 8 5.short s1 = 1; s1 = s1 + 1;有错吗?short s1 = 1; s1 += 1;有错吗? 8 6.Java有没有goto? 8 7.int和Integer有什么区别? 9 8.&和&&的区别? 11

&lt;hdu - 1600 - 1601&gt; Leftmost Digit &amp;&amp; Rightmost Digit 数学方法求取大位数单位数字

1060 - Leftmost Digit 1601 - Rightmost Digit 1060题意很简单,求n的n次方的值的最高位数,我们首先设一个数为a,则可以建立一个等式为n^n = a * 10^x;其中x也是未知的: 两边取log10有:lg(n^n) = lg(a * 10^x); 即:n * lg(n)  - x = lg(a); 现在就剩x一个变量了,我们知道x是值n^n的位数-1,a向下取整就是我们要求的数: 所以 按着上面的推导式翻译成代码就可以了(注意:数值的范围和之间的

3404: [Usaco2009 Open]Cow Digit Game又见数字游戏

3404: [Usaco2009 Open]Cow Digit Game又见数字游戏 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 72  Solved: 48[Submit][Status][Discuss] Description 贝茜和约翰在玩一个数字游戏.贝茜需要你帮助她. 游戏一共进行了G(1≤G≤100)场.第i场游戏开始于一个正整数Ni(l≤Ni≤1,000,000).游 戏规则是这样的:双方轮流操作,将当前的数字减去一个数,这个数可

UVa 1225 / UVALive 3996 Digit Counting 数数字(字符统计)

Digit Counting Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Trung is bored with his mathematics homeworks. He takes a piece of chalk and starts writing a sequence of consecutive integers starting