Recursion in SMath

Recursion in SMath - Can we use recursion in SMath? - Mensajes

#1 Publicado: 24/10/2023 13:28:09
Valery Ochkov

Valery Ochkov

67 me gusta en 713 mensajes.

Grupo: User

Can we use recursion in SMath?
Fibo.png
#2 Publicado: 24/10/2023 14:53:19
Gerry_Bush

Gerry_Bush

12 me gusta en 14 mensajes.

Grupo: User

Yes, recursion is possible. Although I don't remember how many levels are allowed.

Here is your example in an smath file.

Fibo_recursion_example.sm (3,56 KiB) descargado 1507 vez(es).
A 1 usuarios les gustó este mensaje
Valery Ochkov 24/10/2023 16:48:00
#3 Publicado: 24/10/2023 17:13:18
Valery Ochkov

Valery Ochkov

67 me gusta en 713 mensajes.

Grupo: User

Wrote

Yes, recursion is possible. Although I don't remember how many levels are allowed.

Here is your example in an smath file.

Fibo_recursion_example.sm (3,56 KiB) descargado 1507 vez(es).



Thanks! But I think it was not from me but from SMath error!
And the second!
Why 0!=1?
See
Factorial.sm (4,45 KiB) descargado 1493 vez(es).
Factorial.png
#4 Publicado: 24/10/2023 18:23:19
Gerry_Bush

Gerry_Bush

12 me gusta en 14 mensajes.

Grupo: User

Here is the corrected Fibo recursive function...

Fibo_recursion_corrected.sm (4,21 KiB) descargado 1448 vez(es).

As for 0!=1 the following page gives two explanations.

https://zero-factorial.com/whatis.html
#5 Publicado: 24/10/2023 20:21:01
Gerry_Bush

Gerry_Bush

12 me gusta en 14 mensajes.

Grupo: User

Here is a more streamlined version...

Fibonacci function
A 1 usuarios les gustó este mensaje
Valery Ochkov 24/10/2023 20:50:00
#6 Publicado: 24/10/2023 20:49:36
Valery Ochkov

Valery Ochkov

67 me gusta en 713 mensajes.

Grupo: User

Wrote

Here is a more streamlined version...

Fibonacci function



Thanks!
And what about
https://en.wikipedia.org/wiki/Ackermann_function
#7 Publicado: 24/10/2023 21:16:47
Gerry_Bush

Gerry_Bush

12 me gusta en 14 mensajes.

Grupo: User

This seems to work for the Ackermann function...

Ackermann Function

Ackermann function.sm (4,49 KiB) descargado 1442 vez(es).
A 1 usuarios les gustó este mensaje
Valery Ochkov 24/10/2023 22:08:00
#8 Publicado: 25/10/2023 20:32:50
Jean Giraud

Jean Giraud

983 me gusta en 6.866 mensajes.

Grupo: User

Wrote

Yes, recursion is possible. Although I don't remember how many levels are allowed.


Typical, factorial(n) command-line is limited ...factorial(40)
A 1 usuarios les gustó este mensaje
Valery Ochkov 25/10/2023 20:46:00
#9 Publicado: 25/10/2023 21:08:48
Jean Giraud

Jean Giraud

983 me gusta en 6.866 mensajes.

Grupo: User

Wrote

Originally Posted by: Gerry_Bush [url=/forum/yaf_postsm82817_Recursion-in-SMath.aspx#post82817][/url]Yes, recursion is possible. Although I don't remember how many levels are allowed.
Typical, factorial(n) command-line is limited ...factorial(40)


By same token, Fibonacci(n) command-line upper limit
Fibonacci(1473) ... 1.9069*10^307 ... Windows limit.
A 1 usuarios les gustó este mensaje
Valery Ochkov 26/10/2023 0:49:00
#10 Publicado: 25/10/2023 21:15:33
overlord

overlord

600 me gusta en 1.381 mensajes.

Grupo: Moderator

Wrote

Yes, recursion is possible. Although I don't remember how many levels are allowed.


Recursion in SMath has a limit of 50.
After that, it won't calculate.

Regards

2023-10-25_17-01.png
A 2 usuarios les gustó este mensaje
ioan92 25/10/2023 21:26:00, Valery Ochkov 25/10/2023 21:46:00
#11 Publicado: 25/10/2023 22:37:39
Alvaro

Alvaro

1.139 me gusta en 1.805 mensajes.

Grupo: User

Hi. In this post you can see how to handle recursive formulas for more than 50 calls, with a small function rec()

Clipboard01.png

and in this other you can see the relationship between recursive and iterative algorithms, remembering that SMath is optimized for iterative ones.

Clipboard02.png

Best regards.
Alvaro
A 4 usuarios les gustó este mensaje
ioan92 26/10/2023 0:28:00, Valery Ochkov 25/10/2023 23:27:00, NDTM Amarasekera 25/10/2023 23:53:00, Oscar Campo 26/10/2023 16:39:00
#12 Publicado: 26/10/2023 0:33:28
Jean Giraud

Jean Giraud

983 me gusta en 6.866 mensajes.

Grupo: User

Wrote

Typical, factorial(n) command-line is limited ...factorial(40)


The vectorized version n 170 ... 7.2574*10^306

A 1 usuarios les gustó este mensaje
Valery Ochkov 26/10/2023 0:49:00
#13 Publicado: 26/10/2023 2:00:47
Valery Ochkov

Valery Ochkov

67 me gusta en 713 mensajes.

Grupo: User

Gamma.png

#14 Publicado: 26/10/2023 2:02:25
overlord

overlord

600 me gusta en 1.381 mensajes.

Grupo: Moderator

Your RAM and OS is the limit.

Regards

fib_fact.sm (11,01 KiB) descargado 1487 vez(es).

2023-10-25_22-00.png
A 2 usuarios les gustó este mensaje
Valery Ochkov 26/10/2023 2:39:00, Oscar Campo 26/10/2023 16:40:00
#15 Publicado: 26/10/2023 4:07:27
Jean Giraud

Jean Giraud

983 me gusta en 6.866 mensajes.

Grupo: User

Valery, Smath Gamma(x) is known 4 decimals.
Reported years ago, ignored.
Alvaro Gamma(x) 15 decimals
#16 Publicado: 26/10/2023 9:07:36
Alvaro

Alvaro

1.139 me gusta en 1.805 mensajes.

Grupo: User

Wrote

... And what about
https://en.wikipedia.org/wiki/Ackermann_function



Ackermann function is not 'primitive recursive', which means that it can't be computed only with for loops. Here an iterative implementation with a stack and no recursion.

Ackermann-Iterative.sm (8,7 KiB) descargado 1429 vez(es).

Clipboard01.png

Best regards.
Alvaro.
A 2 usuarios les gustó este mensaje
Valery Ochkov 26/10/2023 12:04:00, Oscar Campo 26/10/2023 16:41:00
  • Nuevos mensajes
  • Sin mensajes nuevos