Created
March 24, 2013 00:23
-
-
Save kevincennis/5229873 to your computer and use it in GitHub Desktop.
Euler 25
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fibonacci( n ){ | |
var val = '1', last = '0', tmp, i = 1 | |
for ( ; val.length < n; ++i ){ | |
tmp = val | |
val = add(last, val) | |
last = tmp | |
} | |
return i | |
} | |
function pad( str, len ){ | |
var pre = '', i = 0, max = len - str.length | |
for ( ; i < max; ++i ) pre += '0' | |
return pre + str | |
} | |
function add( s1, s2 ){ | |
var i = s2.length - 1, out = '', rem = val = 0 | |
s1 = pad(s1, s2.length) | |
for ( ; i >= 0; i-- ) { | |
val = parseInt(s1[i], 10) + parseInt(s2[i], 10) + rem | |
rem = val > 9 ? (val = val % 10, 1) : 0 | |
out = val + out | |
} | |
return ( rem || '' ) + out | |
} | |
console.log( fibonacci(1000) ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fibonacci( n ): | |
val = '1' | |
last = '0' | |
tmp = '' | |
i = 1 | |
while ( len(val) < n ): | |
tmp = val | |
val = add(last, val) | |
last = tmp | |
i = i + 1 | |
return i | |
def pad( str, length ): | |
padding = '' | |
i = 0 | |
max = length - len(str) | |
while ( i < max ): | |
padding = padding + '0' | |
i += 1 | |
return padding + str | |
def add( str1, str2 ): | |
i = len(str2) - 1 | |
remainder = 0 | |
result = '' | |
num1 = '' | |
num2 = '' | |
val = 0 | |
str1 = pad(str1, len(str2)) | |
while ( i >= 0 ): | |
num1 = int( str1[i] ) | |
num2 = int( str2[i] ) | |
val = num1 + num2 + remainder | |
if ( val > 9 ): | |
val = val % 10 | |
remainder = 1 | |
else: | |
remainder = 0 | |
result = str(val) + result | |
i = i - 1 | |
if ( remainder != 0 ): | |
result = str(remainder) + result | |
return result | |
print( fibonacci(1000) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment