#Achieving the highest fib subscript number possible

155 messages ยท Page 1 of 1 (latest)

steady prairieBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

red lavaBOT
#
#include> iosteam>
#include "InfInt.h"
#include <chrono>
using namespace std;
using namespace std::chrono;
int main()
{

    const int runLimit{4};

    time_point<steady_clock> start = steady_clock::now();

    time_point<steady_clock> endTime = runLimit * 1s + start; // calculate end time

    int n = 0;

    do
    {
        int term1 = 0;
        int term2 = 1;
        int nextTerm;
        int i = 2;
        while (i = n)
        {
            i++;

            nextTerm = term1 + term2;
            term1 = term2;
            term2 = nextTerm;
            n = i;
        }
    }

    while (steady_clock::now() < endTime);
    cout << n << "\n";
}

```Requested by: dragonslayer0531#0000
gray inlet
#

i = n assigns the value of n to i, not equality check

pure pollen
#

sorry what

#

ohh

gray inlet
#

your while loop condition

#

also, what are you trying to do with that do-while?

#

i feel like this could be simplified

pure pollen
#

well im an amateur- first time coding

#

but i ddint know i could

gray inlet
#

So just since it's your first time, a little advice: Don't use using namespace XYZ;. It's bad practice

#

Issues can arise that are a pita to figure out what the cause is

pure pollen
#

oh okay

#

btw, i just ran it and i get 0 still...

gray inlet
#

yeah, that's just solving one issue in your code

pure pollen
#

oh...

gray inlet
#

try something like this to calc fibb:

long long first = 0;
long long second = 1;
while (...) {
  long long next = first + second;
  first = second;
  second = next;
}
std::cout << second << std::endl;
#

where the ... is the condition you want to continue running your application

pure pollen
#

i have to keep do because of my prof

#

its a part of the template he provided

gray inlet
#

sure, just switch around the do-while

#
do {
  long long next = first + second;
  first = second;
  second = next;
} while (...);
#

what's happening here is that you're checking to make sure i == n for your inner for loop, which will always evaluate to false

#

since you set i = 2 always and n is initially 0. Since you never enter the inner loop of your code, n = i is never called, thus n is always 0 and you can only evaluate the while loop's condition to false

pure pollen
#

also could u explain in easier detail sorry but confusing

gray inlet
#

your debugger did what?

pure pollen
#

it skipped the inner part

#

and went down to while at the bottom

#

skipped while (i=n)

gray inlet
#

So you initially set n to 0, correct? Well, when you do while (i == n), i is equal to 2, right?

#

And n will only ever change if the body of that inner-most while loop is entered

pure pollen
#

yeah

gray inlet
#

So because i == n initially evaluates to false, n will never change, i will always be 2 when you evaluate the while condition, so i == n will always be false

pure pollen
gray inlet
#

correct

#

it doesn't keep track of the subscript, that would be on you to do

#

it just runs and keeps calculating the next value

pure pollen
gray inlet
#

there are fundamental flaws in the logic of your code above

#

i'd recommend thinking about how you'd solve it mathematically, then translate it to code. you'll notice you only need one loop

pure pollen
#

well i would but ive been trying since morning of today and yesterday

#

which i couldve spent studying for othr courses

#

rn im just tryna finish the assignment

#

and i did read the textbook that goes over the basics of these individually but im not a reader

gray inlet
#

welcome to college. learn to read ๐Ÿ˜‰

pure pollen
#

i chose my course cuz of what i wanted to do... not cuz i wanted to learn to code (coding class is mandatory even if u didnt choose it)

gray inlet
#

wrong still

pure pollen
#

after i++; i had written n==i;

#

what

gray inlet
#

why do you have that inner while loop?

pure pollen
#

oh...

#

is it because of the while at the bottom

gray inlet
#

you didn't answer the question. in your mind, what is the reason for your inner while loop?

#

while (i == n);

pure pollen
#

so basically

#

so i left the while

#

as is

#

and i prefer switch/ if else/ for instead of do while cuz i have used them a bit more (since i starteed this sept)

#

but ik theyre basically similar

#

especialy the for

gray inlet
#

still didn't answer my question

#

why while(i == n);

#

i'm trying to follow the reasoning behind your code

pure pollen
#

in my mind i thought of it as: do the "do" portion if i ==n and i left it at that because then it would always be true... (this is only if i assigned n as i in the prior line)

#

honestly even i cant understand

gray inlet
#

so that while isn't associated with the do statement.

pure pollen
#

it is

gray inlet
#

the do-while's while loop is the one after the } of the do block

pure pollen
#

i would go through the do portion first, and make n == i and then have the while statement, which would cause it to go in a loop

#

i think that makes more sense

gray inlet
#

if you can't understand the lines of code you've written, you shouldn't write them ๐Ÿ˜‰

pure pollen
#

when was doing the coding portion, using the template, i didnt factor in that the template is setup so that i didnt need to add a while

gray inlet
#

but no, that while inside the do block will actually never execute (since i == n will always be false)

pure pollen
#

cuz the timer does it for me

gray inlet
#

run through the code by hand

#

will i ever be equal to n?

pure pollen
#

no because i is initialized at 2 and n is at 0, so when it first executes, it does the "do" portion and i increases by 1, but it still isnt = n... so after that it drops dead

#

meaning terminate

#

either both n and i had to start at 0 or 2

#

right...?

gray inlet
#

the inner while stops, but not the do while, yeah

pure pollen
#

well i didnt factor the outside

#

but yeah

#

is that my problem there

#

i got rid of the while

gray inlet
#

you just don't need the inner loop

#

yeah

pure pollen
#

and i also dont need n then either...

#

right

#

cuz nothing to equate

gray inlet
#

correct, no need for n

pure pollen
#

ok

gray inlet
#

just as another quick word of advice for reading and understanding code, single letter variable names aren't useful

#

that appears fine to me, though I'd have to run it to verify

pure pollen
#

that makes sense-

#

i got 159761600

#

seems absurdly big ...

gray inlet
#

print second

#

do:

cout << i << " " << second << "\n";
#

verify that i has the correct second

#

(where i is the subscript of fibbonacci and second is the value at the subscript)

pure pollen
#

ahh okay

gray inlet
#

it's not as big as you think, tbh

#

still pretty damn big though

pure pollen
#

163912554 928101473073031149

#

seems smalll..

#

or i might be dumb

gray inlet
#

you're having the long long wrap around

#

do you need to see how many you can calculate, or the actual number at subscript i?

pure pollen
#

hmmm

#

i need to get the highest subscript

gray inlet
#

that should be fine then

pure pollen
#

hm okay

#

so the value of second is what then?

#

cuz it isnt the actual number right

#

wait is it wrong because long long cant comprehend the massive value of the subscript and so it outputs junk BUT otherwise the subscript itself is right

gray inlet
#

It's because it's larger than the maximum value of a long long, so it's overflowed and wraps back around

pure pollen
#

hm okay

#

what if i instead try infint

gray inlet
#

F(163912554) is an absolutely massive number

pure pollen
#

or can i not use that

gray inlet
#

i have no clue what infint is

pure pollen
#

cuz of idk complications

#

#include "bignumber.h'

#

or synonmys*

gray inlet
#

not a clue what the header is. feel free to try it, though it'll definitely slow down your code

#

(which is expected)

pure pollen
#

oh okay

#

so lower result

#

alright im good with this

#

wait

#

my peers get 20-30k

gray inlet
#

your prof may expect you to use infint's types instead of long long

#

because it can actually support numbers big enough to hold F(n)

pure pollen
#

well he didnt expect us to use infint, rather he didnt specify

gray inlet
#

up to you on what to use then

#

infint will likely slow your code down to what they have

pure pollen
#

ohh

#

ok i think i got it ty ty ty

gray inlet
#

sure, just don't get marked down because second isn't actually correct

#

(because of integer overflow)

pure pollen
#

ima ask my friends, and then adjust

gray inlet
#

๐Ÿ‘

#

good luck

pure pollen
#

ty

steady prairieBOT
#

@pure pollen Has your question been resolved? If so, run !solved :)

pure pollen
#

!solved

steady prairieBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity