Factorial

Factorial  计算阶乘

In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example: 5! = 5 * 4 * 3 * 2 * 1 = 120. By convention the value of 0! is 1.

Write a function to calculate factorial for a given input. If input is below 0 or above 12 throw an exception of type ArgumentOutOfRangeException (C#) or IllegalArgumentException (Java).

More details about factorial can be found here: http://en.wikipedia.org/wiki/Factorial

刚开始没有注意题目的提示,可以直接判断大于12,就溢出了。想到了使用checked关键字来检查是否溢出。

https://msdn.microsoft.com/zh-cn/library/74b4xzyw.aspx   checked关键字的用法

public static class Kata
    {
        public static int Factorial(int n)
        {
            try
            {
                return Recursion(n);
            }
            catch
            {
                throw;
            }
        }

        public static int Recursion(int n)
        {
            if (n < 0)
            {
                throw new ArgumentOutOfRangeException();
            }
            try
            {

                int factorial = 1;
                if (n >= 2)
                {
                    checked
                    {
                        factorial = n * Recursion(n - 1);
                    }
                }
                return factorial;
            }
            catch
            {
                throw new ArgumentOutOfRangeException();
            }
        }
    }

其他人的解法:

public static int Factorial(int n)
  {
    if(n < 0 || n > 12)
      throw new ArgumentOutOfRangeException();
    return n > 0 ? n * Factorial(n - 1) : 1;
  }
using System;
using System.Linq;

public static class Kata
{
  public static int Factorial(int n)
  {
    if(n < 0 || n > 12) throw new ArgumentOutOfRangeException();

    return Enumerable.Range(1, n).Aggregate(1, (x,y) => x * y);
  }
}
时间: 2024-09-28 22:18:34

Factorial的相关文章

LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number

数学题 172. Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. (Easy) 分析:求n的阶乘中末位0的个数,也就是求n!中因数5的个数(2比5多),简单思路是遍历一遍,对于每个数,以此除以5求其因数5的个数,但会超时. 考虑到一个数n比他小

Project Euler:Problem 74 Digit factorial chains

The number 145 is well known for the property that the sum of the factorial of its digits is equal to 145: 1! + 4! + 5! = 1 + 24 + 120 = 145 Perhaps less well known is 169, in that it produces the longest chain of numbers that link back to 169; it tu

[LeetCode]172.Factorial Trailing Zeroes

题目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 分析 朴素解法: 首先求出n!,然后计算末尾0的个数.(重复÷10,直到余数非0) 该解法在输入的数字稍大时就会导致阶乘得数溢出,不足取. O(logn)解法: 考虑n!的质数因子. 后缀0总是由质因子2和质因子5相乘得来的.如果我们可以计数

POJ 1775 sum of Factorial (数论)

链接:http://poj.org/problem?id=1775 Description John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, was a Hungarian-American mathematician who made important contributions to the foundations of mathematics, logic, quantum physics,meteorology, science,

SPOJ FCTRL - Factorial

题目链接:http://www.spoj.com/problems/FCTRL/ 题目大意:询问N的阶乘的结果尾数有几个0. 解题思路:考虑问题:N的阶乘的结果能被2m整除,这个m最大为多少. 我们对前N个数除以2,忽略奇数,会得到N/2个数字.那么相当于我们得到了2N/2 对之后的N/2个数字继续除以2,同样忽略奇数.我们会再得到2N/4 ... 所以m=N/2 + N/4 + N/8 +...+0. 那么对于这个问题,我们计算出N的记成被2m1整除的最大m1以及被5m2整除的最大m2,由于2

Factorial Trailing Zeroes

Factorial Trailing Zeroes Total Accepted: 44612 Total Submissions: 144778 Difficulty: Easy Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. (M) Number of Digit One n!的结果中0的个

Codeforces 515C. Drazil and Factorial

//codeforces 515C. Drazil and Factorial #include <iostream> #include <cstring> #include <cstdio> using namespace std; /** 4!=2!*2!*3! 6!=1*2*3*4*5*6=5!*3! 8!=1*2*3*4*5*6*7*8=7!*2!*2!*2! 9!=1*2*3*4*5*6*7*8*9=7!*2!*3!*3! */ int main() { ch

Hex Factorial 高精度

Hex Factorial 求n的阶乘结果的十六进制表示中,用多少个0.       java秒之! 1 import java.io.*; 2 import java.math.*; 3 import java.util.*; 4 5 public class Main 6 { 7 public static void main(String[] args) 8 { 9 Scanner cin=new Scanner(new BufferedInputStream(System.in)); 1

how many does the factorial of n have zero?

how many does the n factorial end have zero? 个人信息:就读于燕大本科软件工程专业 目前大三; 本人博客:google搜索"cqs_2012"即可; 个人爱好:酷爱数据结构和算法,希望将来从事算法工作为人民作出自己的贡献; 博客内容:how many does the factorial of n have zero? 博客时间:2014-5-5; 编程语言:Java ; 编程坏境:Windows 7 专业版 x64; 编程工具:jdk,ec

light oj 1045 - Digits of Factorial K进制下N!的位数

1045 - Digits of Factorial Factorial of an integer is defined by the following function f(0) = 1 f(n) = f(n - 1) * n, if(n > 0) So, factorial of 5 is 120. But in different bases, the factorial may be different. For example, factorial of 5 in base 8 i