Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recursion broken (fibo.js)? #162

Open
mavavilj opened this issue Jan 27, 2024 · 3 comments
Open

Recursion broken (fibo.js)? #162

mavavilj opened this issue Jan 27, 2024 · 3 comments

Comments

@mavavilj
Copy link

mavavilj commented Jan 27, 2024

Recursion broken (fibo.js)?

var console = require("console");

var fibonacci = function(n)
{
  if (n < 2)
  {
        return n;
  }

        return fibonacci(n - 2) + fibonacci(n - 1);
};
console.log(fibonacci(1));

RESULT:

node ../nerd --run fibo.ng
[*] Generating source file
[*] Compiling with preset: speed
lto-wrapper: warning: using serial compilation of 2 LTRANS jobs

[*] Executing /root/Downloads/nerd-master/example/fibo
1
var console = require("console");

var fibonacci = function(n)
{
  if (n < 2)
  {
        return n;
  }

        return fibonacci(n - 2) + fibonacci(n - 1);
};
console.log(fibonacci(44));

RESULT:

node ../nerd --run fibo.ng
[*] Generating source file
[*] Compiling with preset: speed
lto-wrapper: warning: using serial compilation of 2 LTRANS jobs

[*] Executing /root/Downloads/nerd-master/example/fibo
0

Lowest denominator:

var console = require("console");

var fibonacci = function(n)
{
  if (n < 2)
  {
        return n;
  }

        return fibonacci(n - 2) + fibonacci(n - 1);
};
console.log(fibonacci(3));

node ../nerd --run fibo.ng
[*] Generating source file
[*] Compiling with preset: speed
lto-wrapper: warning: using serial compilation of 2 LTRANS jobs

[*] Executing /root/Downloads/nerd-master/example/fibo
0
@mavavilj
Copy link
Author

No it's not about just recursion, but something about the addition and recursion.

@mavavilj
Copy link
Author

OTOH, e.g. the following version works:

var console = require("console");

var fibonacci = function (num) {
    var num1 = 0;
    var num2 = 1;
    var sum;
    var i = 0;
    for (i = 0; i < num; i++) {
        sum = num1 + num2;
        num1 = num2;
        num2 = sum;
    }
    return num2;
}

console.log("Fibonacci(5): " + fibonacci(5));
console.log("Fibonacci(8): " + fibonacci(8));

@mavavilj
Copy link
Author

The original fibo.ng works with node only:

node fibo.ng

so it's certainly a nerd issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant