Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to determine if a number is a Fibonacci Number?

I've spent a few days trying different things but I can't figure it out. If you're unsure of what a Fibonacci Number is, it's basically an ordering of numbers where the previous two numbers get added together to find the next number in the sequence. Here's some of the beginning sequence,

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ect...

If my definition isn't very good then here is the dictionary's definition,

A series of numbers in which each number ( Fibonacci number ) is the sum of the two preceding numbers. The simplest is the series 1, 1, 2, 3, 5, 8, etc.

Here's a link to the wiki page that provides an equation. I couldn't understand the equation myself. That's one of the things I need help with. I also don't quite understand how to put the equation in Lua. Here's some code sorta explaining what I want to do,

local Number = math.random(1000)

local function Check(Num)
    if Num == Num/2*.5 then--WRONG MATH
        return true
    else 
        return false
end

if Check() then
    print('It is a Fibonacci Number!') 
else
    print("It's not a Fibonacci Number")
end

Where the code says WRONG MATH, is where I don't understand how to fit the number into an equation.

I really wanna know the answer, but how you came to the answer is more valuable. Thank you for your time.

1 comment

2

Just what do you need it for? (I'm simply curious)

1 answer

✓ Accepted answer

Math.
I hate it.

A quick Google search yields this, which is derived from this, which can be translated into Lua as

local function psquare(n)
	local i = math.sqrt(n);
	return math.floor(i) == i
end;
local function isFib(n)
	return psquare(5*n*n+4) or psquare(5*n*n-4)
end

Which should be able to identify Fibonacci numbers.

2 comments

0

Thanks Lunate. This saved me a few extra headaches.

0

thats doing more work than it should

This discussion is preserved as it was. New answers, comments, and votes are closed.