标签: factorial

16
推荐指数
6
解决办法
2万
查看次数

Template factorial function without template specialization

I don't understand the following behavior.

The following code, aimed at computing the factorial at compile time, doesn't even compile:

#include <iostream>
using namespace std;
template<int N>
int f() {
  if (N == 1) return 1; // we exit the recursion at 1 instead of 0
  return N*f<N-1>();
}
int main() {
  cout << f<5>() << endl;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

and throws the following error:

...$ g++ factorial.cpp && ./a.out 
factorial.cpp: In instantiation of ‘int f() [with int N = …
Run Code Online (Sandbox Code Playgroud)

c++ recursion templates factorial template-specialization

16
推荐指数
2
解决办法
297
查看次数

用C++计算大因子

我知道这是一个经典的编程问题,因此我想明确我不是在寻找代码作为解决方案,但我会欣赏正确的方向.我正在学习C++,作为学习过程的一部分,我正在尝试一些编程问题.我正在尝试编写一个程序来处理数十亿的因子.显然,这些将是巨大的数字,并且太大而无法处理使用正常的算术运算.任何迹象表明我应该尝试解决这类问题的方向,我们将不胜感激.

如果可能的话,我宁愿尝试解决这个问题而不使用额外的库

谢谢

PS - 问题出在这里http://www.codechef.com/problems/FCTRL


这是我用来解决问题的方法,这是通过阅读以下评论来实现的:

解决方案 - 数字5是以零结尾的任何数字的主要因子.因此,递归地将阶乘数除以5并添加商,得到阶乘结果中的尾随零数

EG - 126中的尾随零数!= 31

126/5 = 25余数1

25/5 = 5余数0

5/5 = 1余数0

25 + 5 + 1 = 31

这适用于任何值,只需保持分开直到商小于5

c++ integer factorial

15
推荐指数
1
解决办法
7858
查看次数

当使用整数计算Java的阶乘100(100!)时,我得到0

这样做时:

int x = 100;
int result = 1;
for (int i = 1; i < (x + 1); i++) {
    result = (result * i);
}
System.out.println(result);
Run Code Online (Sandbox Code Playgroud)

这显然是因为结果对于整数来说太大了,但我习惯于为溢出得到大的负数,而不是0.

提前致谢!


当我切换到这个:

int x = 100;
int result = 1;

for (int i = 1; i < (x + 1); i++) {
    result = (result * i);
    System.out.println(result);
}
Run Code Online (Sandbox Code Playgroud)

我得到这个.

java int overflow factorial

15
推荐指数
4
解决办法
2万
查看次数

在javascript中找到ith排列

给定一个arr大小的数组n,并且索引0<=i<n!我想返回第i个排列.

我能够编写一个获取所有排列的方法:

function permute (arr) {
  var permutations = [];
  if (arr.length === 1) {
    return [ arr ];
  }

  for (var i = 0; i <  arr.length; i++) { 
    var subPerms = permute(arr.slice(0, i).concat(arr.slice(i + 1)));
    for (var j = 0; j < subPerms.length; j++) {
      subPerms[j].unshift(arr[i]);
      permutations.push(subPerms[j]);
    }
  }
  return permutations;
}
Run Code Online (Sandbox Code Playgroud)

如何修剪它只获得递归的一个分支?

javascript arrays algorithm permutation factorial

15
推荐指数
1
解决办法
1072
查看次数

如何在scala中优化这个短因子函数?(创造50000 BigInts)

我已经比较了scala版本

(BigInt(1) to BigInt(50000)).reduce(_ * _)
Run Code Online (Sandbox Code Playgroud)

到python版本

reduce(lambda x,y: x*y, range(1,50000))
Run Code Online (Sandbox Code Playgroud)

事实证明,scala版本比python版本长了大约10倍.

我猜,一个很大的区别是python可以使用其原生long类型而不是为每个数字创建新的BigInt对象.但scala中有解决方法吗?

optimization scala function factorial lazy-evaluation

14
推荐指数
2
解决办法
2209
查看次数

StackOverflowError计算BigInteger的阶乘?

我正在尝试编写一个Java程序来计算大数的阶乘.似乎BigInteger无法容纳这么大的数字.

以下是我写的(直截了当的)代码.

 public static BigInteger getFactorial(BigInteger num) {
      if (num.intValue() == 0) return BigInteger.valueOf(1);

      if (num.intValue() == 1) return BigInteger.valueOf(1);

      return num.multiply(getFactorial(num.subtract(BigInteger.valueOf(1))));
  }
Run Code Online (Sandbox Code Playgroud)

上述程序在5022中处理的最大数量,之后程序抛出一个StackOverflowError.有没有其他方法来处理它?

java stack-overflow algorithm biginteger factorial

14
推荐指数
1
解决办法
2万
查看次数

Prolog中的可逆数值计算

在阅读SICP时,我遇到了逻辑编程第4.4章.然后我开始研究Prolog编程语言并尝试理解Prolog中的一些简单的任务.我发现Prolog似乎在数值计算方面遇到麻烦.

这是标准Prolog中的阶乘计算:

f(0, 1).
f(A, B) :- A > 0, C is A-1, f(C, D), B is A*D.
Run Code Online (Sandbox Code Playgroud)

我发现的问题是我需要引入两个辅助变量(CD),一个新的语法(is),并且问题是不可逆的(即,f(5,X)按预期工作,但f(X,120)不能).

天真的,我希望至少C is A-1, f(C, D)可以用上面的东西代替f(A-1,D),但即使这样也行不通.

我的问题是:为什么我需要在数值计算中做这些额外的"东西",而不是在其他查询中呢?

我明白(并且SICP非常清楚),一般来说,"做什么"的信息不足以回答"如何做"的问题.因此,(至少一些)数学问题的陈述性知识不足以真正解决这些问题.但这引出了下一个问题:Prolog中这些额外的"东西"如何帮助我将制定局限于那些"做什么"足以回答"如何做"的问题?

prolog factorial clpfd

13
推荐指数
3
解决办法
1558
查看次数

用于循环计算阶乘

目前我有这组代码,它的意思是计算阶乘.

int numberInt = int.Parse(factorialNumberTextBox.Text);

for (int i = 1; i < numberInt; i++)
{
  numberInt = numberInt * i;
}

factorialAnswerTextBox.Text = numberInt.ToString();
Run Code Online (Sandbox Code Playgroud)

由于某种原因,它不起作用,我不知道为什么.例如,我将输入3并得到答案为-458131456,这看起来很奇怪.

任何帮助赞赏.谢谢

c# asp.net loops for-loop factorial

13
推荐指数
2
解决办法
10万
查看次数

无法计算大于20的阶乘!!怎么办?

我使用无符号长整数格式来计算大因子.但是我的代码在某些时候失败了你能看一下吗?实际上它是指数函数的泰勒展开的更大代码的一部分,但是这一部分在这一点上是无关紧要的.我将不胜感激任何建议.

谢谢

#include <stdio.h>
#include <math.h>
//We need to write a factorial function beforehand, since we
//have factorial in the denominators.
//Remembering that factorials are defined for integers; it is
//possible to define factorials of non-integer numbers using
//Gamma Function but we will omit that.
//We first declare the factorial function as follows:
unsigned long long factorial (int);
//Long long integer format only allows numbers in the order of 10^18 so 
//we shall use the sign bit in order to …
Run Code Online (Sandbox Code Playgroud)

c gcc factorial

13
推荐指数
3
解决办法
1万
查看次数