fac
Haskell 中的术语含义是什么?我在很多场合看过它,但似乎没有任何类型的定义.我知道它与阶乘有关,但我不太确定人们在提到这个术语时的意思fac
.
这是一个例子:
sumFacs n = fac 0 + fac 1 + ... + fac (n-1) + fac n
Run Code Online (Sandbox Code Playgroud) 可以使用类似数学的语法定义工作因子函数/运算符吗?即使用!
符号.
我想不出现有符号的任何用例,其中的事情可能是模棱两可的
!shell_escape
总是在一条线的开头爆炸0!=1
会True
像往常一样,因为其他解释factorial(0) = 1
将是SyntaxError: can't assign to literal
或类似的错误)可能重复:
计算任意大数的阶乘,显示所有数字
这个问题可能会在这里问到千次.我正在修改我的问题.我想计算一个大数的阶乘(max range of the number=10^6
).一般来说,我们使用一个for
循环从i=1
到i=number
,每次乘旧值新值.这适用于小数字,但如果我有一个大数字呢?for
循环范围现在增加了.Java原始数据类型int
,long
无法处理由此产生的大量数据.他们只是溢出.虽然我知道BigInteger
类,它可以处理这个大输出,但仍然for
循环不适合我这里.有人可以建议我任何勾选,任何黑客来计算一个数字的阶乘?以下是一个简单的程序,适用于少数 -
public long getFactorial(long number) {
long factorial = 1;
for (long i = 1; i <= number; ++i) {
factorial *= i;
}
return factorial;
}
Run Code Online (Sandbox Code Playgroud) 这个小程序测试了两种计算阶乘的方法 - 迭代和递归.
Factorial.cs:
using System;
namespace Functions
{
public class Factorial
{
public static ulong CalcRecursively(int number)
{
if (number > 1)
return (ulong)number * CalcRecursively(number - 1);
if (number <= 1)
return 1;
return 0;
}
public static ulong Calc(int number)
{
ulong rValue=1;
for (int i = 0; i < number; i++)
{
rValue = rValue * (ulong)(number - i);
}
return rValue;
}
}
}
Run Code Online (Sandbox Code Playgroud)
MainProgram.cs:
using System;
using Functions;
class FunctionClient
{
public static void …
Run Code Online (Sandbox Code Playgroud) 我不能让这个循环执行它应该的阶乘.它似乎只是循环一次.
static void Main(string[] args)
{
int a;
int total=1;
Console.WriteLine("Please enter any number");
a = Convert.ToInt32 (Console.ReadLine());
total = a;
for (int intcount = a; intcount<= 1; intcount--)
{
total *= intcount;
}
Console.WriteLine("Factorial of number '{0}' is '{1}'", a, total);
Console.ReadKey();
Run Code Online (Sandbox Code Playgroud) private void button1_Click(object sender, EventArgs e)
{
String UserNumber = this.textBox1.Text;
int NewUserNumber = Convert.ToInt32(UserNumber);
int result = 0;
int second = 0;
while (NewUserNumber >= 1)
{
result = NewUserNumber * (NewUserNumber - 1);
NewUserNumber--;
}
String i = Convert.ToString(result);
this.textBox2.Text = i;
}
}
Run Code Online (Sandbox Code Playgroud)
虽然我明白这是我的功课,但我被困住了.我真的不想让这个解决,我想自己做.
我不明白为什么它不起作用.无论我输入什么,它都输出2.
我可以很容易地用Java做到这一点,但转换到了我身边.
任何帮助都会很棒.
我正在尝试计算7阶乘的值并显示答案,但是当我试图查找一种方法来执行此操作时,我不断查找编写的代码,以便首先必须从用户输入一些数字,然后它会考虑用户输入的任何数字.但是我已经知道我需要什么数字了,所以代码会有所不同,我无法弄清楚如何做到这一点.
我一开始就尝试过这个
public class Ch4_Lab_7
{
public static void main(String[] args)
{
int factorial = 7;
while (factorial <= 7)
{
if (factorial > 0)
System.out.println(factorial*facto…
factorial--;
}
}
}
Run Code Online (Sandbox Code Playgroud)
但它所做的只是显示7*7,然后是6*6,然后是5*5,依此类推,这不是我想要做的.有谁知道如何正确地做到这一点?
我做了一个递归方法来计算阶乘,但在主方法中我使用了一个for循环来计算阶乘列表.有没有办法在主方法中不使用循环计算阶乘列表?
码:
public class FactInt {
public static void main(String[] args) {
for (int n = 1; n < 11; n++)
System.out.println(factorial(n));
}
//Calculates the factorial of integer n
public static int factorial(int n) {
if (n == 0)
return 1;
else
return n*factorial(n-1);
}
}
Run Code Online (Sandbox Code Playgroud) 我的阶乘计算器工作不正常.
正如我的教授想要的那样,它的预期工作时间为1到20.但是,输入0应该返回1的阶乘; 它返回0
这是我的代码:
private void CalculateFactorial(long number)
{
//perform the calculations
long result = number;
for (int i = 1; i < number; i++)
{
result = result * i;
}
//display the calculated Factorial to the user
txt_Factorial.Text = result.ToString("n0");
}
Run Code Online (Sandbox Code Playgroud)
以下是调用上述方法的方法,即compute计算按钮的事件处理程序:
private void btn_Calculate_Click(object sender, EventArgs e)
{
//get the users input
long number = long.Parse(txt_Number.Text);
// make sure the number not invalid. If it is invalid, tell the user
// otherwise, proceed to calculation.
if (number …
Run Code Online (Sandbox Code Playgroud) 我刚开始编写Haskell编程,主要是因为我在寻找一种比C#更强大的数学语言,现在我很困惑.
现在我正试图简单地找到4的阶乘并打印出来,这是我到目前为止写的:
fact n = product [1..n]
main = do
print fact 4
Run Code Online (Sandbox Code Playgroud)
当我尝试调试它时,我得到了
错误:(3,8)ghc:无法匹配期望的类型
a1 -> t0' with actual type
IO()'函数print' is applied to two arguments, but its type
(a0 - > a0) - > IO()'只有一个在'do'块的stmt中:print fact 4 In表达式:do {print fact 4}
我究竟做错了什么?