Apr 16, 2019 Replies: 6 i had to get the factorial of a number but could not find a buildin function in b4x so i created a function for that. Code: Sub factorial(number As Int) As Int Dim returnNr As Int = number Do Until number <= 1 number = number - 1 returnNr = returnNr * number Loop Return returnNr End Sub use: Code: Log(factorial(6)) log: 720 Click to expand... Last edited: Apr 16, 2019 Click to expand...
i had to get the factorial of a number but could not find a buildin function in b4x so i created a function for that. Code: Sub factorial(number As Int) As Int Dim returnNr As Int = number Do Until number <= 1 number = number - 1 returnNr = returnNr * number Loop Return returnNr End Sub use: Code: Log(factorial(6)) log: 720 Click to expand...
Erel B4X founder Staff member Licensed User Longtime User Apr 16, 2019 #2 Your code is better but it is still worth posting the recursive version as it is quite nice: Code: Sub Factorial(Number As Int) As Int If Number = 1 Then Return 1 Return Number * Factorial(Number - 1) End Sub
Your code is better but it is still worth posting the recursive version as it is quite nice: Code: Sub Factorial(Number As Int) As Int If Number = 1 Then Return 1 Return Number * Factorial(Number - 1) End Sub
ilan Expert Licensed User Longtime User Apr 16, 2019 #3 Erel said: Your code is better but it is still worth posting the recursive version as it is quite nice: Code: Sub Factorial(Number As Int) As Int If Number = 1 Then Return 1 Return Number * Factorial(Number - 1) End Sub Click to expand... really cool, i didnot knew that it is possible to do that with b4x. very smart coding
Erel said: Your code is better but it is still worth posting the recursive version as it is quite nice: Code: Sub Factorial(Number As Int) As Int If Number = 1 Then Return 1 Return Number * Factorial(Number - 1) End Sub Click to expand... really cool, i didnot knew that it is possible to do that with b4x. very smart coding
JordiCP Expert Licensed User Longtime User Apr 16, 2019 #4 As factorial is defined for n>=0 but still 0!=1, it should be (for non-negative numbers) Code: Sub Factorial(Number As Int) As Int If Number=0 Then Return 1 Return Number * Factorial(Number - 1) End Sub
As factorial is defined for n>=0 but still 0!=1, it should be (for non-negative numbers) Code: Sub Factorial(Number As Int) As Int If Number=0 Then Return 1 Return Number * Factorial(Number - 1) End Sub
advansis Active Member Licensed User Longtime User Apr 22, 2019 #5 I prefer non-recursive functions, in this case is very simple to enroll it. Recursive functions increase memory and stack usage. Recursion in very elegant, but, in my own opinion, usually are not efficient
I prefer non-recursive functions, in this case is very simple to enroll it. Recursive functions increase memory and stack usage. Recursion in very elegant, but, in my own opinion, usually are not efficient
Erel B4X founder Staff member Licensed User Longtime User Apr 23, 2019 #6 advansis said: Recursion in very elegant, but, in my own opinion, usually are not efficient Click to expand... You cannot talk about efficiency without a real context. It doesn't matter whether a task takes 0.2ms or 0.4ms. Recursion is a very important tool. Like all tools it has its advantages and disadvantages.
advansis said: Recursion in very elegant, but, in my own opinion, usually are not efficient Click to expand... You cannot talk about efficiency without a real context. It doesn't matter whether a task takes 0.2ms or 0.4ms. Recursion is a very important tool. Like all tools it has its advantages and disadvantages.
advansis Active Member Licensed User Longtime User Apr 28, 2019 #7 Erel said: You cannot talk about efficiency without a real context. It doesn't matter whether a task takes 0.2ms or 0.4ms. Recursion is a very important tool. Like all tools it has its advantages and disadvantages. Click to expand... You are right, Erel. Anyway, when possible I prefer the straigth-way. ;-) Recursion is sometimes misused
Erel said: You cannot talk about efficiency without a real context. It doesn't matter whether a task takes 0.2ms or 0.4ms. Recursion is a very important tool. Like all tools it has its advantages and disadvantages. Click to expand... You are right, Erel. Anyway, when possible I prefer the straigth-way. ;-) Recursion is sometimes misused