# Fibonacci Modified

# Fibonacci Modified

anupmpatil + 10 comments My only concern is problem focusses on handling very large numbers rather than putting it under DP

ktsr42 + 1 comment I agree. Choose a language that has large number support and you can just translate the formula into code and are done. It becomes almost as simple as hello world.

anupkumargupta + 1 comment I want to code in c itself. Any way out ?

peeyushjn108 + 3 comments Yes, Write functions for string multiplication and summation.

padmaa_jaulkar + 1 comment how to do that... plz can u explain??

vasamsettisaiki1 + 4 comments use double to declare data type as double a ,b,c; while scaning scanf("%lf",&a); while printing printf("%0.lf",a); // 0.lf restricts the decimal point to zeroth position which is an integer

padmaa_jaulkar + 0 comments thank you.. thanks allloott... :)

ag19rishu + 2 comments i tried that too. even used long double. that's not working.. can u explain?

kotturusuryaman1 + 1 comment in second test case its a 20 digit and crossing number soo long double is not enough to perform tis programme

mdamircoder + 0 comments [deleted]

pawarashish10 + 0 comments u have to convert number into array/string, like if number is 543656 then store it in array like |3|4|3|6|5|6|. then write seperate function to add and multiply these array/string and then try to find out fibo.

_mHm_ + 0 comments [deleted]rahulchaudhary01 + 2 comments can u help? what is wrong with this????

void fibonacciModified(double t1, double t2, double n) { int i; double arr[20]; arr[0]=t1; arr[1]=t2; for(i=2;i<20;i++) { arr[i]=t1+t2*t2; t1=t2; t2=arr[i]; } for(i=0;i<20;i++) { if((i+1)==n) printf("%0.lf",arr[i]); } }

nidhisingh779 + 0 comments double will not work.. The no. is too large. If you are doing in Java, there is a datatype for very large no. but in C, you have to take the no. as string and do the addition of two no.s by writing a function for addition of two strings.

deamon_2 + 0 comments use python or java big integer because double will not work

dineshkannaa1 + 2 comments i wrote it in c..its giving correct output in devcpp,but here it shows wrong output..

#include<stdio.h> #include<string.h> void stringmul(char*,char*,char*); void stringadd(char*,char*,char*); void swap(char*,char*); void strdrev(char*); int main() { char a[150],b[150]; char mul[150]="",add[150]=""; int l1,l2,i,l,n,j; scanf("%s",a); scanf("%s",b); scanf("%d",&n); for(i=2;i<n;i++) { memset(mul,'\0',150); memset(add,'\0',150); stringmul(b,b,mul); stringadd(mul,a,add); l1=strlen(mul); //printf("a=%s b=%s mul=%s add=%s\n",a,b,mul,add); for(j=0;j<l1;j++) { a[j]=b[j]; b[j]=add[j]; } } printf("%s",add); } void stringmul(char *a,char *b,char *mul) { int i,j,l1,l2,c,c1,temp=0,k,temp1; l1=strlen(a); l2=strlen(b); //printf("(%s,%s)",a,b); strdrev(a); //strdrev(b); //printf("(%s,%s)",a,b); for(i=0;i<l1;i++) { c=0; k=0; c1=0; for(j=0;j<l2;j++) { temp=(a[i]-'0')*(b[j]-'0')+c; if(mul[i+j+k]==0) temp1=(mul[i+j+k])+(temp%10)+c1; else temp1=(mul[i+j+k]-'0')+(temp%10)+c1; mul[i+j+k]=(temp1%10)+'0'; c1=temp1/10; c=temp/10; } if(c1!=0||c!=0) mul[i+j+k]=c1+c+'0'; k++; } strdrev(mul); strdrev(a); } void stringadd(char *a,char *b,char *add) { int i,l1,l2,c,temp=0,p,q; l1=strlen(a); l2=strlen(b); strdrev(a); strdrev(b); int l=l1>l2?l1:l2; c=0; for(i=0;i<l;i++) { temp=0; if(i<l1) temp=a[i]-'0'; if(i<l2) temp=temp+b[i]-'0'; temp=temp+c; add[i]=(temp%10)+'0'; c=temp/10; } if(c!=0) add[i]=c+'0'; strdrev(add); strdrev(a); strdrev(b); } void strdrev(char *a) { int l=strlen(a),i=0; l--; while(i<l) { swap(&a[i],&a[l]); i++; l--; } } void swap(char *a,char *b) { char temp; temp=*a; *a=*b; *b=temp; }

lightingbear + 1 comment programing with the combination of codes and notes can always help. what is the meaning of the sentence like "a[i]-'0'"?

paulkabira + 0 comments converting char a[i] to its decimal equivalent

pawarashish10 + 1 comment same issue getting correct output in my IDE but segmentation fault while submittin... any suggestion?

ravijoshi + 0 comments Can you tell us which testcase is giving seg fault? You can just download that testcase and check yourself too. It must be an edge case or might be using enormous memory.

ashwinkumare994 + 0 comments I get this awesome interesting and unique game which is online and you will love word unscrambler play by using special skills to win this Online Multiplayer game.

earshariff + 0 comments agreee

ccy0013 + 1 comment Although simple enough, I think it still uses the thoguht of DP - it keeps recording the two former numbers and build the solution from bottom to top, instead of recurring from top to bottom, which would lead to a huge number of unnecessary function calls.

Wysaard + 2 comments Well you could do that, but you don't need to. I submitted a solution using dynamic programming, and one using just regular recursion. The difference in running time is so small that it prints the same running times, even though one runs in linear time and the other one in exponential time. They should increase the test size and limit the amount of running time so that without the use of dynamic programming you can't solve the problem imo.

scottm361 + 2 comments This problem doesn't even need recursion.. a for loop can solve this problem too

dude_mil + 1 comment Yes, BigInteger in for loop will solve it.

iamravisankarp + 2 comments how u sloved big integer in for loop??can u explain?

dude_mil + 2 comments Here is the snippet,

`Scanner scn = new Scanner(System.in); BigInteger t1 = new BigInteger(scn.nextInt()+""); BigInteger t2 = new BigInteger(scn.nextInt()+""); int n = scn.nextInt(); BigInteger temp = new BigInteger("0"); for(int i=3;i<=n;i++){ temp = t2; t2 = t2.multiply(t2); t2 = t2.add(t1); t1 = temp; } System.out.println(t2);`

shylajhaa + 1 comment y is it necessary to add "" while initializing the constructor of biginteger ?

tchaikovskyInd + 2 comments Because BigInteger doesn't take int in its constructor but can take a string .So adding "" makes a integer , string .

A nice way is to use valueOf() function of BigInteger class.

shylajhaa + 0 comments oh thank u

jarmanvx64 + 0 comments why not use:

scn.nextBigInteger();

directly?

deepak12345ok + 1 comment my code was correct but how to use biginteger from your code i understands

deamon_2 + 0 comments - static String fibonacciModified(int t1, int t2, int n) { BigInteger c=new BigInteger("0"); BigInteger a=new BigInteger(t1+""); BigInteger b=new BigInteger(t2+""); for (int i=0;i { c=a.add(b.multiply(b)); a=b; b=c; } return c.toString(); }** I hope this will help you

ahmadhasankhan + 2 comments public class Solution { public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ int i,n; BigInteger a,b; Scanner sc=new Scanner(System.in); a = sc.nextBigInteger(); b = sc.nextBigInteger(); n = sc.nextInt(); BigInteger [] val = new BigInteger[n]; val[0] = a; val[1] = b; for(i=2;i<n;i++){ val[i]= (val[i-1].pow(2)).add(val[i-2]); } System.out.println(val[i-1]); } }

anujjindal121 + 0 comments thanks

rlniranjan + 0 comments Compile time error

Compile Message

Solution.java:6: error: cannot find symbol BigInteger a,b; ^ symbol: class BigInteger location: class Solution Solution.java:7: error: cannot find symbol Scanner sc=new Scanner(System.in); ^ symbol: class Scanner location: class Solution Solution.java:7: error: cannot find symbol Scanner sc=new Scanner(System.in); ^ symbol: class Scanner location: class Solution Solution.java:11: error: cannot find symbol BigInteger [] val = new BigInteger[n]; ^ symbol: class BigInteger location: class Solution Solution.java:11: error: cannot find symbol BigInteger [] val = new BigInteger[n]; ^ symbol: class BigInteger location: class Solution 5 errors

egoek + 2 comments I solved it with recursion. Probably stupid but the problem is defined recursively so it was the first thing that popped in my mind. If you memoize the recursive calls it neednÂ´t be terribly inefficient.

metusiast + 1 comment Can you post your recursive solution, please.

barishoku + 1 comment public static BigInteger fiboMod(BigInteger t1,BigInteger t2,int n){ n = n-1; if(n == 1){ return t2; } return fiboMod(t2,t2.pow(2).add(t1),n); }

vivekwisdom + 0 comments This is not DP. Its plain Recursion.

Pvokd + 1 comment The point of doing it in DP and not in recursion is that recursion takes a lot of time while DP doesnt. Doesnt make any sense doing it in recursion as it is said to do it in dp.

andregs + 2 comments I did it using a simple loop and I don't even know what DP is. Could you please show me what a solution using DP looks like?

function main() { let [a, b, n] = input.split(' ').map(Number); a = new BigNumber(a); b = new BigNumber(b); for (let i = 3; i <= n; i++) { var c = next(a, b); a = b, b = c; } process.stdout.write(c.toFixed()); }

IzakRamz + 1 comment DP comes from Dynamic Programming, is an algorithm paradigm or a technique to solve complex problems breaking them into small ones. Just like you I still do not have any idea about what it is or how to implemented. If you find any DP course for dummies, please let me know, the ones I've found are a little bit difficult to follow.

Pvokd + 0 comments https://www.youtube.com/watch?v=P8Xa2BitN3I Check this video out, it might be helpful if you want to understand the basics of Dynamic programming.

frankc + 0 comments Your solution is a DP solution.

You are calculating tn by solving the problem from t1 to tn, and recording the results in order to calculate the one you need.

That's the whole idea of dynamic programming: to solve a complex instance of a problem using the solution to smaller instances.

This problem is so simple that you can't really appreciate the technique. If you want an example of a more "serious" DP algorithm, you might look to a classic like the

**Wagnerâ€“Fischer algorithm**to compute the distance between strings. It's simple enough but not trivial.

KCBorys + 1 comment Yeah, Python makes this problem pretty trivial

hdennen + 1 comment True, I switched from JavaScript just to get it done.. did find a pitfall though.

third = math.pow(second, 2) + first

will produce weird numbers from the 8th iteration.

instead must use: second*second + first

Soultaker + 1 comment That's because

`math.pow()`

will use floating-point numbers. It is more typically used when the power is a real number, not (necessarily) an integer.To raise integers to an integer power, use the exponentiation operator

`**`

:`third = second**2 + first`

(Of course to compute a square of

`x`

,`x*x`

works just fine as well.)marco1893 + 0 comments [deleted]

jbrewer + 0 comments [deleted]billo + 3 comments can u pls explain how to handle big integer in c++.

jbrewer + 1 comment AFAIK, there's no easy way to do this problem in C++ with the libraries provided. In real life you'd use a bignum library like GNU MP or Boost.Multiprecision, but those libraries aren't available here.

You can certainly roll your own, but that seems a little extreme.

I've done all the other algorithm problems in C++, but for this one I switched to Java so I could use java.math.BigInteger.billo + 0 comments ohk.thank you

sonle_hut + 4 comments Here is how I do it. You can actually make it faster by playing with Base.

`#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; namespace { char toChar(char d) { return d | 0x30; } } constexpr unsigned Base = 100; class BigNumber { private: string m_num; public: BigNumber(int num = 0); BigNumber(const BigNumber & other); BigNumber(BigNumber && other); BigNumber &operator+=(const BigNumber & num2); void swap(BigNumber &num); BigNumber &operator=(BigNumber num); friend BigNumber operator*(const BigNumber & num1, const BigNumber & num2); friend BigNumber operator+(BigNumber num1, const BigNumber & num2); friend ostream & operator<<(ostream & outstream, const BigNumber & num); private: void add(const BigNumber &num, int exp); }; BigNumber::BigNumber(int num) { for (; num; num /= Base) { m_num.push_back(num % Base); } if (m_num.empty()) { m_num.push_back(0); } } BigNumber::BigNumber(const BigNumber & other) : m_num(other.m_num) { } BigNumber::BigNumber(BigNumber && other) : m_num(std::move(other.m_num)) { } BigNumber operator*(const BigNumber & num1, const BigNumber & num2) { BigNumber precomp[Base]; for (int i = 1; i < Base; ++i) { precomp[i] = precomp[i - 1] + num1; } BigNumber result; int exp = 0; for_each(num2.m_num.begin(), num2.m_num.end(), [&result, &precomp, &exp](char c) { result.add(precomp[c], exp++); }); return result; } void BigNumber::add(const BigNumber & num, int exp) { if (exp > m_num.size()) { m_num.append(exp - m_num.size(), 0).append(num.m_num.begin(), num.m_num.end()); } else { auto b1 = m_num.begin() + exp, e1 = m_num.end(); auto b2 = num.m_num.begin(), e2 = num.m_num.end(); int r = 0; for (; b1 != e1 && b2 != e2; ++b1, ++b2, r/= Base) { r += *b1 + *b2; *b1 = r % Base; } for (; r && b1 != e1; ++b1, r /= Base) { r += *b1; *b1 = r % Base; } for (; r && b2 != e2; ++b2, r /= Base) { r += *b2; m_num.push_back(r % Base); } if (b2 != e2) { m_num.append(b2, e2); } if (r) { m_num.push_back(r); } } } BigNumber &BigNumber::operator+=(const BigNumber & num2) { add(num2, 0); return *this; } BigNumber operator+(BigNumber num1, const BigNumber & num2) { num1.add(num2, 0); return num1; } ostream & operator<<(ostream & outstream, const BigNumber & num) { string x(num.m_num.size() * 2, 0); for (int i = 0; i < num.m_num.size(); ++i) { auto d = num.m_num[i]; x[2 * i] = toChar(d % 10); x[2 * i + 1] = toChar(d / 10); } if (x.back() == '0') { x.erase(x.size() - 1); } reverse(x.begin(), x.end()); return outstream << x; } BigNumber & BigNumber::operator=(BigNumber num) { m_num = std::move(num.m_num); return *this; } void BigNumber::swap(BigNumber & num) { m_num.swap(num.m_num); } int main() { int _a, _b, n; cin >> _a >> _b >> n; BigNumber a(_a), b(_b); while (n-- > 2) { a += b * b; a.swap(b); } cout << b; return 0; }`

Kishan1997 + 0 comments well done man

nikhilgoyal9 + 1 comment Please can you explain your code

IzakRamz + 0 comments I'm sorry, I got lost in the conversation, which code?

abhicatjee + 1 comment you are awesome man

kotturusuryaman1 + 0 comments # include

# include

# include

# include

int main() { int i=2,x,y,n; int z,a[100]={0}; scanf("%d %d %d",&x,&y,&n);

`while(i<n)`

{ z=0; z=x+pow(y,2); x=y; y=z; i++; a[z]+=z; }

printf("%d",a[z]); return 0; }this my code it is displaying segementation error can anyone help me in getting the actual bug in this code

sai4992 + 0 comments I barely know a thing or two about that ostream operator,great work man!!

vladimir_petrigo + 3 comments I passed all tests by using the Karatsuba multiplication algorithm with my own Big Integer implementation. Very dumb by the way, but fast enough to find the 24 modified Fibonacci number in a half of second (0 1 24). Nothing special work was done about working with number representation â€“ store it as is in a base 10 in a vector. If it is still relevant for you, email me (taenaru@gmail.com) and I'll gladly explain you how it might work.

satishm752 + 0 comments [deleted]nickholden786 + 0 comments can you send me explaination

nickholden786@gmail.com

vitaly_ivanov + 0 comments It's a waste of dev time, imho. Karatsuba algo replaces 2mul+1add with 1mul+4add. They say mul takes 3-4 times cycles of add these days, so you gonna get max 12.5% speed up (or zero, if it's actually 3 times) for the code which probably takes less than 20% of execution time (memory access will be the bottleneck, not number crunching)

kotturusuryaman1 + 1 comment what is the function that is to be used in c to handle such big integer like this 84266613096281243382112? help me in doing this

dionisioedu + 0 comments There is not! And this is the challenge. You will need to cut the number in pieces to work with it.

polarbearrito + 0 comments [deleted]ElizaW + 0 comments Same concern. Luckily I'm on Python.

mark163 + 0 comments Totally agree. This really shouldn't be categorized as a dynamic programming problem. Without having to worry about big numbers the solution is trival.

For people using C#, just use System.Numerics.BigInteger.

s_aleksandras + 6 comments C++ and c has a distinct disadvantage for this problem since they don't have builtin big integer support, why not modulo 10e9+7 output as is customary for big number problems :/

tyler24_nl + 1 comment is there a solution now?

GMP is not included by default here

evolving_again + 1 comment You can use integer array to store values as in--- Extra Long Factorials--https://www.hackerrank.com/challenges/extra-long-factorials Small factorials question on spoj--http://www.spoj.com/problems/FCTRL2/

SteppenWolfe + 1 comment Using C++, i have successfully completed extra long factorials challenge, getting all desired factorial outputs, yet for this problem, my implementation using my previos BigInt class causing "termination due to timeout". I guess i should alter my overloading method for * operator, but i can't figure out how. I can get the result of 2147483647*999999 operation, but timeout occurs for 2147483647*9999999.

evolving_again + 1 comment I would still recommend checking out the integer array method it would help you greatly

SteppenWolfe + 2 comments Actually for implementing BigInt class in C++, i have used an integer array (vector of ints in fact), and to implement multiply operation, I have overloaded the addition operation of BigInt as well. I can multiply a very very large number with a relatively very small number, as in the case of calculating 25! = 25 * 24!; i can multiply ,for example, 24! with 25 but not with say 1000000, i get timeout in such cases.

softarts + 4 comments no c++ solution yet...I saw all the solution in java or python or else except C++.

riveridea + 1 comment you can filter the solution to find the C/C++ solution. They use the cpp_int to define the variable.

SeamanTarfu + 4 comments I'm not sure how I should understand that, but did they copied part of boost to own programs? Because it seems that boost is not supported here, isn't it?

rincerwind + 3 comments A C++ solution would involve implementing the addition and multiplication of Big Integers. I can do it, if people are interested in such a solution.

duttamoumita23 + 0 comments yes i am interested.. plz help.

manavkothari + 0 comments i m interested.

karanpathak + 1 comment Use

**__int128**datatype.manualdidact + 1 comment If you look at some of the later testcases, you'll notice that some go

*way*beyond the capacity of a 128-bit integer, signed or unsigned. This exercise requires nothing less than true arbitrary-precision integer math.Skym0sh0 + 0 comments Thats what I found out a couple of minutes ago.

I used __int128 and 3 or 4 of my testcases were false. I checked and rechecked it, but I couldnt imagine I did something wrong.

So I just jumped over to Java and its built-in BigInteger stuff and bam, it worked.

So for every future solver: The numbers do not only exceed 64 Bit, they also exceed 128 Bit !

DibyaRB + 0 comments I tried biginteger implemenataion by Anudeep Neekanti.

Passed all test cases except 6. Weird.

andrii_sukhariev + 0 comments https://ideone.com/or4YD8 all test cases were accepted hope it helps

DibyaRB + 1 comment I tried biginteger implemenataion by Anudeep Neekanti.

Passed all test cases except 6. Weird.

dongxuli2011 + 2 comments I tried a lazy implementation of about 50 lines of C++ for a big integer struct and passed all test cases:

algoguy + 0 comments I agree. The inability to handle large integer has nothing to do with dynamic programming. This question is not under a suitable category.

krywen + 0 comments I've passes it doing my own C++ BigInt implementation. I've noticed however that users on the top of the scoreboard used #include which seems to be no longer available during compilation.

Kot_Zadrot + 1 comment Well, as a C++ developer you are supposed to know a bit more then say an average Python developer. So although it is sick that standard library misses BigInt, you might find it really useful to implement some limited version of BigInt of your own.

krywen + 1 comment I think is part of the game choosing the most appropriate language for each problem.

Kot_Zadrot + 0 comments That is definitely true for solving the real-world problems but not for learning. Do this problem in Python and you will not learn anything. Do it in your not so lucky language without the big int, and I bet you'll get a bit better coder.

vixir + 5 comments I found this good tutorial for Java programmers - http://compsci.ca/v3/viewtopic.php?t=13193 this will help.

shantanu1395 + 0 comments Really helpful!!!

santoshmaley + 0 comments [deleted]rahul_goswami02 + 0 comments I learnt something new today! Thank you my friend!

I0000 + 0 comments Thank you for this!

MijaelWatts + 0 comments Thanks a lot man, I learned a lot with that tutorial

yuzman + 6 comments With C# I'm having issues with 2 test cases, where printing is exceeding 6 seconds.

Braini + 0 comments Same here. On my local machine it is calculated within a second, so I wonder if the server is restricted in resources or something like that.

Jamespin + 0 comments I have the same issue.

shashank21jHackerRank Admin + 0 comments You should read the comments in the bottom

http://pastebin.com/F1MDkU2Jkosmur + 0 comments same problem :( with c# I belive that solution is classic even use less memo than in tutorial :(

muratcatal + 1 comment The samething happened also with me. Code runs on local fast, however server says exceeding 6 seconds.

atique + 1 comment I was also surprised regarding TLE using C#. It turns out there are many ways to solve that using C#. Importantly, you have to consider that converting BigInteger to String using ToString is slow since it has to perform division operation on the big integer everytime to get a single digit. Therefore, following approaches work mentioned in the code here on github works great.

Acknowledgement for the idea to following

Ehouarn + 0 comments Haven't experienced anything bad with BigInteger, but I may have been just lucky. I was wondering if there was any other way to solve this challenge other than just using the "brute force".

bhajanpreet25 + 1 comment Same here.... But in my local machine it works just fine.

sandeepdude + 0 comments Test case 2 and 6 takes only a little more than a second to run. Try using BigInteger data type and store the previous values in an array(memoization technique).

marinskiy + 0 comments Here is

**Python 3**solution from my HackerrankPractice repository:n1, n2, n = map(int, input().split()) sequence = [n1, n2] if n <= 2: print(sequence[n-1]) else: for i in range(n-2): sequence.append(sequence[-2] + sequence[-1]**2) print(sequence[-1])

Feel free to ask if you have any questions :)

fewcha + 1 comment How come it is that there are people who imported the boost/multiprecision library to solve the problem using C++ and got full score, but when I try to run the same code on my machine, it is not being able to compile the line #include "boost/multiprecision/cpp_int.hpp".

Isn't Hackerrank supposed to use the very same compiler to a C++ code? How come it is running for him, and not for me? Is there some settings I need to change, that I'm not aware of?

abhiranjan + 1 comment Hi @fewcha, boost libraries, which were mistakenly enabled earlier, are disabled now. We have disbaled boost because of vast libraries avaiable in it. They is not ideal for available challenges in other subdomains (specicially maths heavy one). Sorry for that.

aakside + 1 comment Is any library available to provide support for large integers in C++?

rchapin + 1 comment There are, but they are not provided for us in hackerrank. I found the same thing as others who attempted to solve the problem with C++; that the task becomes more about how to implement a big integer class instead of solving a very simple dynamic programming problem. That being said, it was a very good exercise. Unfortunately, my solution is not yet optimized enough to perform well enough after about 18 or so iterations.

Just for the heck of it, I wrote up a solution in Java. 5 minutes and 41 lines later a solution that passes all of the test cases by using the BigInteger class which is a part of the JDK.

I think those that solve the problem in C/C++ (or any other language that does not include a big integer implementation) should get extra credit for a complete solution.

robbyoconnor + 1 comment Why not do it in python or Ruby, or even Java...push yourselves!

Ayelis + 1 comment I've got this far in JavaScript and by god if I'm not going to program my own bigint function to handle this damn problem.

robbyoconnor + 1 comment The laziness is epic here.

aakside + 0 comments I don't think you're being fair.

Regardless of whether he learns a language that simplifies the task he's trying to do, or if he expends effort figuring out a solution with what he already knows, there's not much laziness. It's challenging, admirable, and beneficial either way.

With regards to your parent question: There's no implication we aren't using or didn't use Python, Ruby or Java. We're merely discussing whether or not a certain kind of library is included in the test environment. We're not whining.

dassu + 0 comments It's kinda funny when problem statement encourages submissions to be in C++ and the Editorial is provided in JAVA(obviously used BigInteger) LOL.

marungshanew + 3 comments Can some help me? I m getting value 8.426661309628124e+22 with javascript for 1st test case. How to overcome this problem ? I tried this http://stackoverflow.com/questions/10943997/how-to-convert-a-string-containing-scientific-notation-to-correct-javascript-num , but not working.

selby + 1 comment You won't be able to solve this using any JS native functions. To solve this problem, you can spin up your own big-num helpers(pretty messy for a problem of this scale). In the real world you might use a library like https://github.com/jtobey/javascript-bignum.

Michael_Hobbs + 1 comment var BigNumber = require('bignumber.js');

HOWEVER! This problem appears to be to big for BigNumber. Thus JS is out the window for this problem unless you want to try to roll a version of BigNumber that wont break on this.

DraigRygbi + 4 comments Not too big for BigNumber. I just did it.

`var BigNumber = require('bignumber.js');`

You need to set it to change where it switches to displaying exponents instead of whole numbers.

`BigNumber.config({ EXPONENTIAL_AT: 1e+9 })`

Also, make it not limit the precision when using powers.

`BigNumber.config({ POW_PRECISION: 0 })`

And then it works! :-D

gabo77zurdex + 1 comment Hi, I added that configuration but I get this error: BigNumber Error: new BigNumber() number type has more than 15 significant digits: 8.426661309628124e+22

nrajan + 1 comment so you found the fix for this ?

gabo77zurdex + 0 comments No, I use java instead.

rmilejcz + 0 comments ah you're my hero! thanks mate

nrajan + 1 comment nope, got something like:

BigNumber Error: new BigNumber() number type has more than 15 significant digits:

petrgazarov + 0 comments I got that when I was using

`Math.pow`

. Use`BigNumber.toPower`

instead.

nrajan + 0 comments [deleted]

squirrelMsger + 0 comments even it is converted to print without scientific notation, the precision is different

DraigRygbi + 0 comments Use BigNumber!

`var BigNumber = require('bignumber.js');`

You need to set it to change where it switches to displaying exponents instead of whole numbers.

`BigNumber.config({ EXPONENTIAL_AT: 1e+9 })`

Also, make it not limit the precision when using powers.

`BigNumber.config({ POW_PRECISION: 0 })`

And then it works! :-D

Here's where you can find out how to use it: http://mikemcl.github.io/bignumber.js/

rgclinton + 2 comments I tried this in C# and was timed out on test case #3 and #6. Using Java with the same code almost verbatim it passes all tests. BigInteger in Java must be a better implementation than the Mono implimentation. :-/

PRASHANTB1984Challenge Author + 3 comments you could even try implementing your own compound data structures - using 2 or more integers to represent a customized BigInt!

rgclinton + 2 comments Thanks for the suggestion! Sounds like a lot of extra work to me though and I'm kind of lazy like that. I'll try that out in the future but for now I'm just going to take my points from the Java implementation and declare victory. As it is the important part is only about 10 lines of code. This isnt the first challenge I've done where it works fine in one language but not another and all i really want to do is warn fellow C# programmers to use Java if C# isnt fast enough. Happy Hacking!

rbraunz + 1 comment Thanks for the heads up. I've just spent the last 10 minutes bashing my head on why I can't get the adequate performance on test cases 3 and 6 in c#. Had I not seen this comment I probably would've wasted much more time.

shashank21jHackerRank Admin + 1 comment http://pastebin.com/F1MDkU2J here's the code in C#

rbraunz + 0 comments Thanks for posting this. For whatever reason I had missed the comment below about Console.WriteLine being slow. That helps tremendously.

atique + 0 comments C# is fast enough. hackerrank is not using a good C# compiler.

bartolomy + 0 comments [deleted]bartolomy + 0 comments This test should be removed as it is very bias to the language choice. You choose by accident language you like which doesn't have bigint prebuilt and you are going to loose a lot time.

shashank21jHackerRank Admin + 2 comments Console.WriteLine on Big Integers is very slow.

Here's a trick

https://www.hackerrank.com/challenges/fibonacci-modified/submissions/code/2599736sophiasun + 2 comments Can you share your trick here? I can't access your code.

shashank21jHackerRank Admin + 0 comments Converting to string and printing

shashank21jHackerRank Admin + 0 comments

Jamespin + 0 comments Thanks so much for sharing that trick!

dowlet505 + 0 comments Python3 only 1 line

return (t2 if n==2 else fibonacciModified(t2,t1+t2*t2,n-1))

Sort 478 Discussions, By:

Please Login in order to post a comment