big1 = 100000
big2 = 150000
Print gcd(big1, big2)
Print lcd(big1, big2)
Print lcd2(big1, big2)
WaitKey
Function gcd(A,B)
While B > 0
R=A Mod B
A=B
B=R
Wend
Return A
End Function
Function lcd(A,B)
C=(A*B)/gcd(A,B)
Return C
End Function
Function lcd2(A,B)
Return A * ( B / gcd(A,B) )
End Function
Function fac(n)
check=1
For i=1 To n
check=check*i
Next
Return check
End Function
Print "4! = "+fac(4)
Print "3! = "+fac(3)
Print "-1! = "+fac(-1)
Print "-4! = "+fac(-4)
Print "0! = "+fac(0)
Function Fac%(n%)
If(Not(n%))Then Return 0
Local nReturn%=1
Local nIter%=1
For nIter%=1 To n%
nReturn%=nReturn%*nIter%
Next
Return nReturn%
End Function
Function nCk%(n%,k%)
If (k%>0) And (k<n%) Then Return Fac(n%)/(Fac(k%)*Fac(n-k%))
Return 0
End Function
Function Gcd%(A%,B%)
Local n%=0
If (B%<1) Then Return 0
While (B%)
n=A% Mod B%
A%=B%
B%=n%
Wend
Return A%
End Function
I'm not sure what "your" Heron's algorithm is trying to do, but as it stands, it doesn't even follow Heron's formula or stops itself from looping to infinity. You have thrown in values like x1# for no reason whatsoever. Thus it fails on pretty much everything.Recommend using Heron's ACTUAL algorithm:fCurrent#=0.5*(fLast#+(n%/fLast#))Using this formula limited by n iterations (It should never need that many, but makes a good prevention of infinite loops) gives a pretty good return.Dunnno whether it's faster or slwoer than Blitz' default Sqr() or even, whether or not it gives as good accuracy.Function Sqrt#(n%)
Local fCurrent#=n%
Local fLast#=0.5*(fCurrent#+(n%/fCurrent#))
Local nIter%=1
While ((nIter)*(nIter%<n))
nIter%=nIter%+1
fCurrent#=0.5*(fLast#+(n%/fLast#))
fLast#=fCurrent#
Wend
Return fLast
End Function
Note for future reference: Test your code a bit before submitting it ;) Function nCr( n, r ) ; binomial coefficient "n choose r"
If r > n/2 Then Return nCr( n, n - r )
c = 1
For d = 1 To r
c = ( c * ( n +1 - d ) ) / d
Next
Return c
End Function