试图创建一个rutine来找到一个数字的阶乘。 [英] Trying to create a rutine to find the factorial of a number.

查看:79
本文介绍了试图创建一个rutine来找到一个数字的阶乘。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这就是我要做的事情:


我想创建一个从文本框中获取值的rutine。让我们说

例5.并且以下5 * 4 * 3 * 2 * 1 = 120

rutine应该可以取任何数字并做同样的事情:


如果是9那么它应该乘以:9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1


如果它是3那么它应该乘以:3 * 2 * 1


我正在使用这个但是没有得到我想要的东西:


函数因子(ByVal N As Integer)作为整数

如果N< = 1则''达到递归调用的结束。

返回1''N = 0或1,所以退出电话。

Else''N> 1,所以再次致电Factorial。

返回因子(N - 1)* N

结束如果

结束功能

如果有人有任何建议,


谢谢,

解决方案

Eduardo78写道:

好的,这就是我要做的事情:

我想创建一个从文本框中获取值的rutine。让我们说
例5.并且以下5 * 4 * 3 * 2 * 1 = 120
这个rutine应该能够取任何数字并做同样的事情:

如果它是9那么它应该乘以:9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1
如果它是3那么它应该乘以:3 * 2 * 1

我正在使用这个但是没有得到我想要的东西:

功能因子(ByVal N As Integer)作为整数
如果N< = 1然后''达到递归呼叫的结束。
返回1''N = 0或1,所以爬回呼叫。
否则'N> 1,所以再次致电Factorial。
返回因子(N - 1)* N
结束如果
结束功能

如果有人有任何建议,




看起来像标准算法,它对我来说正常工作。为什么

它没有按预期工作?

-

Gregory Gadow


< blockquote> Eduardo78,


你不喜欢你的代码怎么样?它似乎给出了你用作例子的值的正确答案。


Kerry Moorman

" Eduardo78"写道:

好的,这就是我要做的事情:

我想创建一个从文本框中获取值的rutine。让我们说
例5.并且以下5 * 4 * 3 * 2 * 1 = 120

rutine应该能够取任何数字并做同样的事情:

如果它是9那么它应该乘以:9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1
如果它是3那么它应该乘以:3 * 2 * 1

我正在使用这个但是没有得到我想要的东西:

功能因子(ByVal N As Integer)作为整数
如果N< = 1然后''达到递归呼叫的结束。
返回1''N = 0或1,所以爬回呼叫。
否则'N> 1,所以再次致电Factorial。
返回因子(N - 1)* N
结束如果
结束功能

如果有人有任何建议,

谢谢,



>我正在使用这个,但没有得到我想要的东西:


函数因子(ByVal N As Integer)作为整数
如果N< = 1那么''达到了结束递归呼叫。
返回1''N = 0或1,所以爬出呼叫。
否则'N> 1,所以再次致电Factorial。
返回因子(N - 1)* N
结束如果
结束功能




你的因子功能是正确的,所以我不明白你的意思

没得到我想要的东西。您可能希望将Integer更改为Long,因为

您将溢出一个具有相当小的N值的整数。(某处接近

10表示整数,20表示长整数) 。或者,使用Double,Double将

有效地保持精度,因为阶乘可被大数除以

2的幂。


如果你需要非常大数的阶乘,那么就要牺牲

精度并使用LogGamma函数来返回Gamma函数的自然对数

。 br />


Ok, this is what i am tying to do:

I want to create a rutine that takes a value from a text box. Lets say for
example 5. and does the following 5*4*3*2*1 = 120
the rutine should be able to take any number and do the same:

if it is 9 then it should multiply: 9*8*7*6*5*4*3*2*1

if it is 3 then it should multiply: 3*2*1

I was working with this one but didnt get what i wanted:

Function Factorial(ByVal N As Integer) As Integer
If N <= 1 Then '' Reached end of recursive calls.
Return 1 '' N = 0 or 1, so climb back out of calls.
Else '' N > 1, so call Factorial again.
Return Factorial(N - 1) * N
End If
End Function
If someone has any suggestions,

Thanks,

解决方案

Eduardo78 wrote:

Ok, this is what i am tying to do:

I want to create a rutine that takes a value from a text box. Lets say for
example 5. and does the following 5*4*3*2*1 = 120

the rutine should be able to take any number and do the same:

if it is 9 then it should multiply: 9*8*7*6*5*4*3*2*1

if it is 3 then it should multiply: 3*2*1

I was working with this one but didnt get what i wanted:

Function Factorial(ByVal N As Integer) As Integer
If N <= 1 Then '' Reached end of recursive calls.
Return 1 '' N = 0 or 1, so climb back out of calls.
Else '' N > 1, so call Factorial again.
Return Factorial(N - 1) * N
End If
End Function

If someone has any suggestions,



That looks like the standard algorithm, and it is working correctly for me. Why
is it not working as you expect?
--
Gregory Gadow


Eduardo78,

What about your code don''t you like? It seems to give the right answer for
the values you used as examples.

Kerry Moorman
"Eduardo78" wrote:

Ok, this is what i am tying to do:

I want to create a rutine that takes a value from a text box. Lets say for
example 5. and does the following 5*4*3*2*1 = 120
the rutine should be able to take any number and do the same:

if it is 9 then it should multiply: 9*8*7*6*5*4*3*2*1

if it is 3 then it should multiply: 3*2*1

I was working with this one but didnt get what i wanted:

Function Factorial(ByVal N As Integer) As Integer
If N <= 1 Then '' Reached end of recursive calls.
Return 1 '' N = 0 or 1, so climb back out of calls.
Else '' N > 1, so call Factorial again.
Return Factorial(N - 1) * N
End If
End Function
If someone has any suggestions,

Thanks,



> I was working with this one but didnt get what i wanted:


Function Factorial(ByVal N As Integer) As Integer
If N <= 1 Then '' Reached end of recursive calls.
Return 1 '' N = 0 or 1, so climb back out of calls.
Else '' N > 1, so call Factorial again.
Return Factorial(N - 1) * N
End If
End Function



Your Factorial function is correct, so I don''t understand what you mean by
"didnt get what i wanted". You might want to change Integer to Long, because
you will overflow an integer with a fairly small value of N. (Somewhere near
10 for integer, 20 for long). Alternatively, use Double, and Double will
effectively retain precision because factorials are divisible by large-ish
powers of two.

If you need factorials of very large numbers, the way to go is to sacrifice
precision and use the LogGamma function which returns the natural logarithm
of the Gamma function.


这篇关于试图创建一个rutine来找到一个数字的阶乘。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆