我正在尝试在 vbscript 中实现一些递归.
I am trying to implement some recursion in vbscript.
Function largest_prime_factor (ByVal num) For i = 2 to num/2 If num mod i = 0 Then 'this number is not prime largest_prime_factor (num / i) End If Next largest_prime_factor = num ''if at this point, we have reached the largest prime End Function如您所见,它是一个脚本,基本上旨在为我提供一个数字的最大质因数.然而,当我运行打印时,这个脚本仍然向我吐出推算的数字.调试后,我发现脚本确实会在 for 循环中输入条件,但是它不会递归(即:它将继续通过 for 循环运行,然后在该点之后结束)
As you can see, it is a script that is basically designed to give me the largest prime factor of a number. However, this script still spits back the imputed number at me when I run a print. After debugging, I have found that the script will indeed enter the conditional inside the for loop, but then it will NOT recurse (ie: it will keep running through the for loop and then just end after that point)
我对 VBscript 中的递归有什么误解?我也尝试了一些
What did I miss about recursion in VBscript? I also tried something to the effect of
largest_prime_factor = largest_prime_factor (num / i)在条件中,这也不起作用.
Inside the conditional and this didn't work either.
推荐答案在上面发布的代码中,您犯了两个小错误
In code posted above, you have made two minor mistakes
工作代码---
Function largest_prime_factor (ByVal num) For i = 2 to num/2 If num mod i = 0 Then 'this number is not prime num= largest_prime_factor (num / i) Exit For End If Next largest_prime_factor = num ''if at this point, we have reached the largest prime End Function更多推荐
VBscript 的递归问题
发布评论