For loop VBscript的递归问题
我试图在vbscript中实现一些递归For loop VBscript的递归问题,for-loop,recursion,vbscript,For Loop,Recursion,Vbscript,我试图在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
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循环中运行,然后在该点之后结束)
关于VBscript中的递归,我错过了什么?我还尝试了一些效果很好的东西
largest_prime_factor = largest_prime_factor (num / i)
在条件中,这也不起作用。在上面发布的代码中,您犯了两个小错误
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