Sie sind auf Seite 1von 35

<!DOCTYPE html> <!-- saved from url=(0086)http://stackoverflow.

com/questions/9439352/codility-fi bonacci-solution/9439806#9439806 --> <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>c# - Codility Fibonacci Solution - Stack Overflow</title> <link rel="shortcut icon" href="http://cdn.sstatic.net/stackoverflow/img/fav icon.ico"> <link rel="apple-touch-icon image_src" href="http://cdn.sstatic.net/stackove rflow/img/apple-touch-icon.png"> <link rel="search" type="application/opensearchdescription+xml" title="Stack Overflow" href="http://stackoverflow.com/opensearch.xml"> <script type="text/javascript" async="" src="./c# - Codility Fibonacci Solut ion - Stack Overflow_files/adzerk1_2_4_43,adzerk2_2_17_45,adzerk3_2_4_44"></scri pt><script type="text/javascript" async="" src="./c# - Codility Fibonacci Soluti on - Stack Overflow_files/ga.js"></script><script type="text/javascript" async=" " src="./c# - Codility Fibonacci Solution - Stack Overflow_files/quant.js"></scr ipt><script type="text/javascript" src="./c# - Codility Fibonacci Solution - Sta ck Overflow_files/jquery.min.js"></script> <script type="text/javascript" src="./c# - Codility Fibonacci Solution - Sta ck Overflow_files/stub.js"></script> <link rel="stylesheet" type="text/css" href="./c# - Codility Fibonacci Solut ion - Stack Overflow_files/all.css"> <link rel="canonical" href="http://stackoverflow.com/questions/9439352/codil ity-fibonacci-solution"> <link rel="alternate" type="application/atom+xml" title="Feed for question & #39;Codility Fibonacci Solution&#39;" href="http://stackoverflow.com/feeds/quest ion/9439352"> <script type="text/javascript"> StackExchange.ready(function () { StackExchange.using("postValidation", function () { StackExchange.postValidation.initOnBlurAndSubmit($('#post-form') , 2, 'answer'); }); StackExchange.question.init({showAnswerHelp:true,totalCommentCount:6 ,shownCommentCount:5,highlightColor:'#F4A83D',backgroundColor:'#FFF',questionId: 9439352}); styleCode(); StackExchange.realtime.subscribeToQuestion('1', '9439352'); }); </script> <script type="text/javascript"> StackExchange.init({"stackAuthUrl":"https://stackauth.com","serverTime": 1372754430,"styleCode":true,"enableUserHovercards":true,"site":{"name":"Stack Ov erflow","description":"Q&A for professional and enthusiast programmers","isNotic esTabEnabled":true,"recaptchaPublicKey":"6LdchgIAAAAAAJwGpIzRQSOFaO0pU6s44Xt8aTw c","enableSocialMediaInSharePopup":true},"user":{"fkey":"9336b12e86af343415687d9 e813eec5f","isAnonymous":true}}); StackExchange.using.setCacheBreakers({"js/prettify-full.js":"6c261bebf56

a","js/moderator.js":"68a5f45d34d6","js/full-anon.js":"761e0ff892e6","js/full.js ":"b16edc73d2d1","js/wmd.js":"2f79c03846d5","js/third-party/jquery.autocomplete. min.js":"e5f01e97f7c3","js/mobile.js":"e8e23ad37820","js/help.js":"6e6623243cf6" ,"js/tageditor.js":"450c9e8426fc","js/tageditornew.js":"b6c68ad4c7dd","js/inline -tag-editing.js":"8e84e8a137f7","js/revisions.js":"7273bb714bba","js/review.js": "59e9b73580be","js/tagsuggestions.js":"aa48ef6154df","js/post-validation.js":"bb 996020492a","js/explore-qlist.js":"1c5bbd79b562"}); </script> <script type="text/javascript"> StackExchange.using("gps", function() { StackExchange.gps.init(true); }); </script> <script type="text/javascript"> StackExchange.ready(function () { $('#nav-tour').click(function () { StackExchange.using("gps", function() { StackExchange.gps.track("aboutpage.click", { aboutclick_ location: "headermain" }, true); }); }); }); </script> <script async="" type="text/javascript" src="./c# - Codility Fibonacci Solution - Stack Overflow_files/full-anon.js"></script><script type="text/javascript">//c dn test - don't delete! </script><script async="" type="text/javascript" src="./c# - Codility Fibonacci Solution - Stack Overflow_files/post-validation.js"></script><script async="" ty pe="text/javascript" src="./c# - Codility Fibonacci Solution - Stack Overflow_fi les/prettify-full.js"></script><script type="text/javascript" src="./c# - Codili ty Fibonacci Solution - Stack Overflow_files/adFeedback.js"></script><link rel=" stylesheet" href="http://static.adzerk.net/Extensions/adFeedback.css"></head> <body class="question-page"><div style="position: absolute;"><object id="_GPL_sw f" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://downloa d.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="1" height="1"><param name="movie" value="http://contentcache-a.akamaihd.net/items/ it/swf/f.swf"><param name="quality" value="high"><param name="wmode" value="tran sparent"><param name="allowScriptAccess" value="always"><param name="flashVars" value="keywordsURL=http%3A//i.tracksrv.com/kwd%3Fc%3DOjo6c3RhY2tvdmVyZmxvdy5jb20 6ei0xMDIxLTE3MzQ5MQ%253D%253D%26cb%3D_GPL.items.a652c.displayKeywords&amp;keywor ds=stack%20overflow%7Ca%20question%20and%20answer%20site%20for%20professional%20 and%20enthusiast%20programmers%7Cit%27s%7Cfree%7Cno%20registration%20required%7C trying%20to%7Cthe%20codility%20self-certification%20at%7Cthe%20question%7Cas%20f ollows%7Cthe%20fibonacci%20sequence%7Cdefined%20by%20the%20following%20recursive %20formula%7Cwrite%20a%20function%7Cint%7Cthat%7Cgiven%20two%20non-negative%20in tegers%7Creturns%20a%20remainder%20of%7Cmodulo%7C000%7C103%7Cnote%7Ca%20prime%20 number%7Cfor%20example%7Cthe%20function%20should%20return%7Csince%7Cassume%20tha t%7Care%20integers%20within%20the%20range%7Ccomplexity%7Cexpected%20worst-case%2 0time%20complexity%7Clog%7Cexpected%20worst-case%20space%20complexity%7Chere%7Cm y%20solution%7Cpublic%7Cdouble%7Cmath%7Cconsole%7Cwriteline%7Cmodfib%7Cconvert%7 Ctoint64%7Cint64%7Clong%7Ctemp%7Cmod%7Ctoint32%7Cproblem%7Cmy%20solution%20fails %20for%20very%20large%20numbers%7Cthe%20solution%20failed%20on%20test%7Cwith%20w rong%20answer%7Cgot%7Casked%7Cfeb%7Cwhat%7Cyou%20mean%7Cfails%20due%20to%20takin g%20too%20long%7Cyou%20receive%20an%20error%7Cmissing%20something%7Cusing%20the% 7Ctype%20will%20result%20in%20a%20loss%20of%20precision%20for%20very%20large%20n umbers%7Cmeaning%20that%20your%20output%20will%20not%20match%20the%20expected%7C letting%20the%20so%20community%20help%20you%20solve%20this%20problem%20means%7Cu nable%20to%20solve%7Con%20your%20own%7Cin%20other%20words%7Cmeans%20you%20cheat%

20and%7Cget%20that%20certificate%7Cbut%20hey%7Cat%20least%7Chonest%7Creally%20wa nt%20the%20certificate%7Cneed%20to%7Cone%20of%20these%20codility%20tests%20over% 20the%20weekend%20for%20a%20job%20application%20so%20want%20to%20practice%7Con%2 0one%20of%20the%20other%20sample%20tests%20but%20this%20one%20has%20me%20stumped %7Ctwo%20things%7Cnot%20use%20any%20floating%20point%20numbers%20for%20this%7Cle arn%20some%20basic%20number%20theory%20if%20you%20really%20want%20to%20solve%20t his%20problem%7Cgood%20luck%7Caccepted%7Cstart%20by%20thinking%20about%20the%20r ange%20of%20the%20problem%7Cso%20max%7Cyour%20solution%7Cthat%20many%20additions %7Cwhich%20will%20take%20some%20time%7Cstore%20the%20result%7Cfar%7Cfar%20larger %7Cin%20a%20double%7Cthat%20has%20a%20maximum%20range%20of%20about%7Conly%20accu rate%20to%7Csignificant%20digits%20anyways%7Cyou%20need%20to%20come%20up%20with% 20a%20better%20solution%7Cstart%20by%20learning%20about%20modular%20arithmetic%7 Chere%27s%20a%20hint%7Csuppose%20you%20want%20to%20compute%7Ctell%20you%20right% 20now%7Cit%27s%20one%7Chow%20did%7Cthat%20so%20fast%7Cbecause%20if%20a%20mod%7Ct hen%7Ccongruent%20to%7Cand%20similarly"><!--[if !IE]> <--> <object id="_GPL_swf" data="http://contentcache-a.akamaihd.net/items/it/swf/f.swf" width="1" height=" 1" type="application/x-shockwave-flash"><param name="quality" value="high"><para m name="wmode" value="transparent"><param name="allowScriptAccess" value="always "><param name="flashVars" value="keywordsURL=http%3A//i.tracksrv.com/kwd%3Fc%3DO jo6c3RhY2tvdmVyZmxvdy5jb206ei0xMDIxLTE3MzQ5MQ%253D%253D%26cb%3D_GPL.items.a652c. displayKeywords&amp;keywords=stack%20overflow%7Ca%20question%20and%20answer%20si te%20for%20professional%20and%20enthusiast%20programmers%7Cit%27s%7Cfree%7Cno%20 registration%20required%7Ctrying%20to%7Cthe%20codility%20self-certification%20at %7Cthe%20question%7Cas%20follows%7Cthe%20fibonacci%20sequence%7Cdefined%20by%20t he%20following%20recursive%20formula%7Cwrite%20a%20function%7Cint%7Cthat%7Cgiven %20two%20non-negative%20integers%7Creturns%20a%20remainder%20of%7Cmodulo%7C000%7 C103%7Cnote%7Ca%20prime%20number%7Cfor%20example%7Cthe%20function%20should%20ret urn%7Csince%7Cassume%20that%7Care%20integers%20within%20the%20range%7Ccomplexity %7Cexpected%20worst-case%20time%20complexity%7Clog%7Cexpected%20worst-case%20spa ce%20complexity%7Chere%7Cmy%20solution%7Cpublic%7Cdouble%7Cmath%7Cconsole%7Cwrit eline%7Cmodfib%7Cconvert%7Ctoint64%7Cint64%7Clong%7Ctemp%7Cmod%7Ctoint32%7Cprobl em%7Cmy%20solution%20fails%20for%20very%20large%20numbers%7Cthe%20solution%20fai led%20on%20test%7Cwith%20wrong%20answer%7Cgot%7Casked%7Cfeb%7Cwhat%7Cyou%20mean% 7Cfails%20due%20to%20taking%20too%20long%7Cyou%20receive%20an%20error%7Cmissing% 20something%7Cusing%20the%7Ctype%20will%20result%20in%20a%20loss%20of%20precisio n%20for%20very%20large%20numbers%7Cmeaning%20that%20your%20output%20will%20not%2 0match%20the%20expected%7Cletting%20the%20so%20community%20help%20you%20solve%20 this%20problem%20means%7Cunable%20to%20solve%7Con%20your%20own%7Cin%20other%20wo rds%7Cmeans%20you%20cheat%20and%7Cget%20that%20certificate%7Cbut%20hey%7Cat%20le ast%7Chonest%7Creally%20want%20the%20certificate%7Cneed%20to%7Cone%20of%20these% 20codility%20tests%20over%20the%20weekend%20for%20a%20job%20application%20so%20w ant%20to%20practice%7Con%20one%20of%20the%20other%20sample%20tests%20but%20this% 20one%20has%20me%20stumped%7Ctwo%20things%7Cnot%20use%20any%20floating%20point%2 0numbers%20for%20this%7Clearn%20some%20basic%20number%20theory%20if%20you%20real ly%20want%20to%20solve%20this%20problem%7Cgood%20luck%7Caccepted%7Cstart%20by%20 thinking%20about%20the%20range%20of%20the%20problem%7Cso%20max%7Cyour%20solution %7Cthat%20many%20additions%7Cwhich%20will%20take%20some%20time%7Cstore%20the%20r esult%7Cfar%7Cfar%20larger%7Cin%20a%20double%7Cthat%20has%20a%20maximum%20range% 20of%20about%7Conly%20accurate%20to%7Csignificant%20digits%20anyways%7Cyou%20nee d%20to%20come%20up%20with%20a%20better%20solution%7Cstart%20by%20learning%20abou t%20modular%20arithmetic%7Chere%27s%20a%20hint%7Csuppose%20you%20want%20to%20com pute%7Ctell%20you%20right%20now%7Cit%27s%20one%7Chow%20did%7Cthat%20so%20fast%7C because%20if%20a%20mod%7Cthen%7Ccongruent%20to%7Cand%20similarly"></object> <!---> <!--[endif]----> </object></div><div style="position: absolute; top: 0px; le ft: 0px; width: 1px; height: 1px; z-index: 2147483647;" id="_GPL_e6a00_parent_di v"><object type="application/x-shockwave-flash" id="_GPL_e6a00_swf" data="http:/ /contentcache-a.akamaihd.net/items/e6a00/storage.swf?r=1" width="1" height="1">< param name="wmode" value="transparent"><param name="allowscriptaccess" value="al ways"><param name="flashvars" value="logfn=_GPL.items.e6a00.log&amp;onload=_GPL. items.e6a00.onload&amp;onerror=_GPL.items.e6a00.onerror&amp;LSOName=gpl"></objec

t></div> <noscript>&lt;div id="noscript-padding"&gt;&lt;/div&gt;</noscript> <div id="notify-container"></div> <div id="overlay-header"></div> <div id="custom-header"></div> <div class="container"> <div id="header" class="headeranon"> <div id="portalLink"> <a class="genu" onclick="StackExchange.ready(function(){genuwine .click();});return false;">Stack Exchange</a> </div> <div id="topbar"> <div id="hlinks"> <span id="hlinks-user"></span> <span id="hlinks-nav"> <a href="http://stackoverflow.com/ users/login?returnurl=%2fquestions%2f9439352%2fcodility-fibonacci-solution%2f943 9806">sign up</a> <span class="lsep">|</span> <a href="http://stackoverflow.com/users/login?returnurl=%2fq uestions%2f9439352%2fcodility-fibonacci-solution%2f9439806">log in</a> <span class="lsep">|</span> <a href="http://careers.stackoverflow.com/">careers 2.0</a> <span class="lsep">|</span> </span> <span id="hlinks-custom"></span> </div> <div id="hsearch"> <form id="search" action="http://stackoverflow.com/search" m ethod="get" autocomplete="off"> <div> <input autocomplete="off" name="q" class="textbox" p laceholder="search" tabindex="1" type="text" maxlength="240" size="28" value=""> </div> </form> </div> </div> <br class="cbt"> <div id="hlogo"> <a href="http://stackoverflow.com/"> Stack Overflow </a> </div> <div id="hmenus"> <div class="nav mainnavs mainnavsanon"> <ul> <li class="youarehere"><a id="nav-questions" href="h ttp://stackoverflow.com/questions">Questions</a></li> <li><a id="nav-tags" href="http://stackoverflow.com/ tags">Tags</a></li> <li><a id="nav-tour" href="http://stackoverflow.com/ about">Tour</a></li> <li><a id="nav-users" href="http://stackoverflow.com /users">Users</a></li> </ul> </div>

<div class="nav askquestion"> <ul> <li> <a id="nav-askquestion" href="http://stackoverflow.c om/questions/ask">Ask Question</a> </li> </ul> </div> </div> </div>

<div id="content"> <div itemscope="" itemtype="http://schema.org/Article"> <link itemprop="image" href="http://cdn.sstatic.net/stackoverflow/img/apple-touc h-icon.png"> <!--googleoff: snippet--> <div id="herobox-mini"> <div id="hero-content"> <span id="controls"> <a href="http://stackoverflow.com/about" id="tell-me-more" class="bu tton">Tell me more</a> <span id="close"><a title="click to dismiss"></a></span> </span> <div id="blurb"> <span id="site-name">Stack Overflow</span> is a question and answer site for professional and enthusiast programmers. It's 100% free, no registra tion required. </div> </div> <script> $('#tell-me-more').click(function () { var clickSource = $("body").attr("class") + '-mini'; if ($("body").hasClass("questions-page")) { clickSource = 'questionpagemini'; } if ($("body").hasClass("home-page")) { clickSource = 'homepagemini'; } StackExchange.using("gps", function () { StackExchange.gps.track("aboutpage.click", { aboutclick_location : clickSource } , true); }); }); $('#herobox-mini #close').click(function () { StackExchange.using("gps", function () { StackExchange.gps.track("hero.action", { hero_action_type: "clos e" }, true); }); $.cookie("hero", "none", { path: "/" }); var $hero = $("#herobox-mini"); $hero.slideUp('fast', function () { $hero.remove(); }); return false; });

</script> </div> <!--googleon: snippet--> <div id="question-header"> <h1 itemprop="name"><a href="http://stackoverflow.com/questions/9439352/codi lity-fibonacci-solution" class="question-hyperlink">Codility Fibonacci Solution< /a></h1> </div> <div id="mainbar">

<div class="question" data-questionid="9439352" id="question"> <div class="everyonelovesstackoverflow adzerk-vote" id="adzerk1" ><a href="http://engine.adzerk.net/r?e=eyJhdiI6NDE0LCJhdCI6NCwiY20iOjg0NywiY2giO jExNzgsImNyIjo1OTIxLCJkaSI6ImU0ZjY3ZDg0ZWUyZjQzNmFiMDAxOGYzYmY0OTk1YzdkIiwiZG0iO jEsImZjIjo4ODA3LCJmbCI6MjQ0NCwia3ciOiJjIyxmaWJvbmFjY2ksY29kaWxpdHkiLCJudyI6MjIsI nJmIjoiaHR0cDovL3N0YWNrb3ZlcmZsb3cuY29tL3RhZ3MvY29kaWxpdHkvaG90IiwicnYiOjAsInByI joxNTY4LCJzdCI6ODI3Nywiem4iOjQzLCJ1ciI6Imh0dHA6Ly9jYXJlZXJzLnN0YWNrb3ZlcmZsb3cuY 29tLyJ9&s=5a_b442KOoGY-oOLkrlvF8ox3uI" rel="nofollow" target="_blank" title="">< img src="./c# - Codility Fibonacci Solution - Stack Overflow_files/9241eb4107b24 c588c0ca0042534c03e.png" title="" alt="" border="0" width="728" height="90"></a> <div class="adzerk-vote-controls" style=""><div class="adzerk-vote-option adzerk -vote-up"><div class="adzerk-vote-icon"></div></div><div class="adzerk-vote-opti on adzerk-vote-down"><div class="adzerk-vote-icon"></div></div></div><div class= "adzerk-vote-survey" style="display:none;"><form><span>No problem. We won't show you that ad again. Why didn't you like it?</span><ul><li><label><input type="ra dio" value="12" name="downvoteReason">Uninteresting</label></li><li><label><inpu t type="radio" value="13" name="downvoteReason">Misleading</label></li><li><labe l><input type="radio" value="14" name="downvoteReason">Offensive</label></li><li ><label><input type="radio" value="15" name="downvoteReason">Repetitive</label>< /li></ul><a href="http://stackoverflow.com/questions/9439352/codility-fibonaccisolution/9439806#" class="adzerk-vote-cancel">Oops! I didn't mean to do this.</a ></form></div><img height="0px" width="0px" border="0" src="./c# - Codility Fibo nacci Solution - Stack Overflow_files/i.gif"></div> <table> <tbody><tr> <td class="votecell"> <div class="vote"> <input type="hidden" value="9439352"> <a class="vote-up-off" title="This question shows research effort; it is use ful and clear">up vote</a> <span class="vote-count-post ">2</span> <a class="vote-down-off" title="This question does not show any research eff ort; it is unclear or not useful">down vote</a> <a class="star-off" href="http://stackoverflow.com/questions/9439352/codilit y-fibonacci-solution/9439806#" title="This is a favorite question (click again t o undo)">favorite</a> <div class="favoritecount"><b>7</b></div> </div> </td>

<td class="postcell"> <div> <div class="post-text" itemprop="description"> <p>I'm trying to do the Codility self-certification at : <a href="http:/ /codility.com/cert/start/" rel="nofollow">http://codility.com/cert/start/</a></p > <p>The Question is as follows:</p> <p>The Fibonacci sequence is defined by the following recursive formula:</p> <p>F(0) = 0 </p> <p>F(1) = 1 </p> <p>F(N) = F(N-1) + F(N-2) for N 2 </p> <p>Write a function:</p> <p>int power_fib(int N, int M);</p> <p>that, given two non-negative integers N and M, returns a remainder of F(N^M) modulo 10,000,103. </p> <p>Note: 10,000,103 is a prime number.</p> <p>For example, given N = 2 and M = 3, the function should return 21, since 2^3 = 8 and F(8) = 21. Assume that:</p> <p>N and M are integers within the range [0..10,000,000].</p> <p>Complexity: expected worst-case time complexity is O(log(N*M));</p> <p>expected worst-case space complexity is O(1).</p> <p>Here is my solution:</p> <pre class="lang-cs prettyprint prettyprinted" style=""><code><span class="pln"> </span><span class="kwd">public</span><span class="pln"> </span><span class= "typ">int</span><span class="pln"> power_fib</span><span class="pun">(</span><sp an class="typ">int</span><span class="pln"> N</span><span class="pun">,</span><s pan class="pln"> </span><span class="typ">int</span><span class="pln"> M</span>< span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">double</span><span class="pln"> x </span><span class="pun">=</span><span class="pln"> </span><span class="typ">Math</span><span class="pun">.</span><span class="typ">Pow</span><span class="pun">(</span><span class="pln">N</span><span class="pun">,</span><span class="pln"> M</span><span class="pun">);</span><span class="pln"> </span><span class="typ">Console</span><span class="pun">.</span><span c lass="typ">WriteLine</span><span class="pun">(</span><span class="pln">x</span>< span class="pun">);</span><span class="pln"> </span><span class="typ">int</span><span class="pln"> modfib </span><spa n class="pun">=</span><span class="pln"> </span><span class="typ">ModFib</span>< span class="pun">(</span><span class="typ">Convert</span><span class="pun">.</sp an><span class="typ">ToInt64</span><span class="pun">(</span><span class="pln">x </span><span class="pun">));</span><span class="pln">

</span><span class="kwd">return</span><span class="pln"> modfib</span><s pan class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">public</span><span class="pln"> </span><span class= "typ">int</span><span class="pln"> </span><span class="typ">ModFib</span><span c lass="pun">(</span><span class="typ">Int64</span><span class="pln"> n</span><spa n class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">double</span><span class="pln"> a </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span cl ass="pun">;</span><span class="pln"> </span><span class="kwd">double</span><span class="pln"> b </span><span class="pun">=</span><span class="pln"> </span><span class="lit">1</span><span cl ass="pun">;</span><span class="pln"> </span><span class="kwd">for</span><span class="pln"> </span><span class ="pun">(</span><span class="kwd">long</span><span class="pln"> i </span><span cl ass="pun">=</span><span class="pln"> </span><span class="lit">0</span><span clas s="pun">;</span><span class="pln"> i </span><span class="pun">&lt;</span><span c lass="pln"> n</span><span class="pun">;</span><span class="pln"> i</span><span c lass="pun">++)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">double</span><span class="pln"> temp </span ><span class="pun">=</span><span class="pln"> a</span><span class="pun">;</span> <span class="pln"> a </span><span class="pun">=</span><span class="pln"> b</span><span class="pun">;</span><span class="pln"> b </span><span class="pun">=</span><span class="pln"> temp </span><s pan class="pun">+</span><span class="pln"> b</span><span class="pun">;</span><sp an class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">var</span><span class="pln"> mod </span><span c lass="pun">=</span><span class="pln"> a</span><span class="pun">%</span><span cl ass="lit">10000103</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">return</span><span class="pln"> </span><span cl ass="typ">Convert</span><span class="pun">.</span><span class="typ">ToInt32</spa n><span class="pun">(</span><span class="pln">mod</span><span class="pun">);</sp an><span class="pln"> </span><span class="pun">}</span></code></pre> <p>Problem is, my solution fails for very large numbers:</p> <p>The solution failed on test: (4, 7) with WRONG ANSWER [ got -2147483648 expected 909213 ] </p> </div> <div class="post-taglist"> <a href="http://stackoverflow.com/questions/tagged/c%23" class="post-tag " title="show questions tagged &#39;c#&#39;" rel="tag">c#</a> <a href="http://st ackoverflow.com/questions/tagged/fibonacci" class="post-tag" title="show questio ns tagged &#39;fibonacci&#39;" rel="tag">fibonacci</a> <a href="http://stackover flow.com/questions/tagged/codility" class="post-tag" title="show questions tagge d &#39;codility&#39;" rel="tag">codility</a> </div> <table class="fw"> <tbody><tr>

<td class="vt">

<div class="post-menu"><a href="http://stackoverflow.com/q/9439352" title="short permalink to this question" class="short-link" id="link-post-9439352">share</a> <span class="lsep">|</span><a href="http://stackoverflow.com/posts/9439352/edit" class="suggest-edit-post" title="">improve this question</a></div> </td> <td align="right" class="post-signature"> <div class="user-info "> <div class="user-action-time"> <a href="http://stackoverflow.com/posts/9439352/revi sions" title="show all edits to this post">edited <span title="2012-02-25 16:05: 33Z" class="relativetime">Feb 25 '12 at 16:05</span></a> </div> <div class="user-gravatar32"> </div> <div class="user-details"> <br> </div> </div> </td> <td class="post-signature owner"> <div class="user-info "> <div class="user-action-time"> asked <span title="2012-02-24 23:09:10Z" cla ss="relativetime">Feb 24 '12 at 23:09</span> </div> <div class="user-gravatar32"> <a href="http://stackoverflow.com/users/843437/kev847382"><div class =""><img src="./c# - Codility Fibonacci Solution - Stack Overflow_files/add9e035 f80de74fbf1667a731d147e1" alt="" width="32" height="32"></div></a> </div> <div class="user-details"> <a href="http://stackoverflow.com/users/843437/kev847382">kev847382< /a><br> <span class="reputation-score" title="reputation score" dir="ltr">26 </span><span title="1 silver badge"><span class="badge2"></span><span class="bad gecount">1</span></span><span title="6 bronze badges"><span class="badge3"></spa n><span class="badgecount">6</span></span> </div> </div> </td> </tr>

</tbody></table> </div> </td> </tr> <tr> <td class="votecell"></td> <td> <div id="comments-9439352" class="comments"> <table> <tbody> <tr id="comment-11937716" class="comment"> <td></td> <td class="comment-text"><div><span class="comment-copy">What do you mea n it fails due to taking too long? Do you receive an error?</span> &nbsp;<a href ="http://stackoverflow.com/users/1224415/matt-t" title="401 reputation" class="c omment-user">Matt T</a> <span class="comment-date" dir="ltr"><a class="comment-l ink" href="http://stackoverflow.com/questions/9439352/codility-fibonacci-solutio n/9439806#comment11937716_9439352"><span title="2012-02-24 23:18:38Z" class="rel ativetime-clean">Feb 24 '12 at 23:18</span></a></span></div></td> </tr> <tr id="comment-11937851" class="comment"> <td></td> <td class="comment-text"><div><span class="comment-copy">I assume you're m issing something. Using the <code>double</code> type will result in a loss of pr ecision for very large numbers, meaning that your output will not match the expe cted.</span> &nbsp;<a href="http://stackoverflow.com/users/1149773/douglas" title ="14809 reputation" class="comment-user">Douglas</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="http://stackoverflow.com/questions/94393 52/codility-fibonacci-solution/9439806#comment11937851_9439352"><span title="201 2-02-24 23:27:21Z" class="relativetime-clean">Feb 24 '12 at 23:27</span></a></sp an></div></td> </tr> <tr id="comment-11937928" class="comment"> <td class="comment-actions"><table><tbody><tr> <td class="comment-score"> <span title="number of &#39;useful comment&#39; votes received" class="cool">4</ span> </td> <td> &nbsp; </td></tr> </tbody></table></td> <td class="comment-text"><div><span class="comment-copy">I assume you're missing something. letting the SO community help you solve this problem means y ou're unable to solve it <b>on your own</b>. In other words it means you cheat a nd shouldn't get that certificate. But hey, at least you're honest.</span> &nbsp; <a href="http://stackoverflow.com/users/43959/kaii" title="6311 reputation" clas s="comment-user">Kaii</a> <span class="comment-date" dir="ltr"><a class="comment -link" href="http://stackoverflow.com/questions/9439352/codility-fibonacci-solut ion/9439806#comment11937928_9439352"><span title="2012-02-24 23:33:25Z" class="r elativetime-clean">Feb 24 '12 at 23:33</span></a></span></div></td> </tr> <tr id="comment-11937951" class="comment"> <td></td>

<td class="comment-text"><div><span class="comment-copy">I don't really want the certificate - but I need to do one of these Codility Tests over the wee kend for a job application so want to practice. I got 100% on one of the other sample tests but this one has me stumped.</span> &nbsp;<a href="http://stackoverf low.com/users/843437/kev847382" title="26 reputation" class="comment-user owner" >kev847382</a> <span class="comment-date" dir="ltr"><a class="comment-link" href ="http://stackoverflow.com/questions/9439352/codility-fibonacci-solution/9439806 #comment11937951_9439352"><span title="2012-02-24 23:35:10Z" class="relativetime -clean">Feb 24 '12 at 23:35</span></a></span></div></td> </tr> <tr id="comment-11938079" class="comment"> <td></td> <td class="comment-text"><div><span class="comment-copy">Two things: 1. Do not use any floating point numbers for this. 2. Learn some basic number theor y if you really want to solve this problem. 3. Good luck.</span> &nbsp;<a href="h ttp://stackoverflow.com/users/1011995/daniel-fischer" title="99353 reputation" c lass="comment-user">Daniel Fischer</a> <span class="comment-date" dir="ltr"><a c lass="comment-link" href="http://stackoverflow.com/questions/9439352/codility-fi bonacci-solution/9439806#comment11938079_9439352"><span title="2012-02-24 23:42: 27Z" class="relativetime-clean">Feb 24 '12 at 23:42</span></a></span></div></td> </tr> </tbody> </table> </div> <a id="comments-link-9439352" class="comments-link" data-comments-count="1" title="expand to show all comments on this post">show <b>1</b> more comment</a> </td> </tr> </tbody></table> </div> <div id="answers"> <a name="tab-top"></a> <div id="answers-header"> <div class="subheader answers-subheader"> <h2> 3 Answers </h2> <div id="tabs"> <a href="http://stackoverflow.com/questions/9439352/codility-fib onacci-solution?answertab=active#tab-top" title="Answers with the latest activit y first">active</a> <a href="http://stackoverflow.com/questions/9439352/codility-fibonacci-solution? answertab=oldest#tab-top" title="Answers in the order they were provided">oldest </a> <a class="youarehere" href="http://stackoverflow.com/questions/9439352/codilityfibonacci-solution?answertab=votes#tab-top" title="Answers with the highest scor e first">votes</a> </div> </div> </div>

<a name="9439806"></a> <div id="answer-9439806" class="answer accepted-answer" data-answerid="9439806" style=""> <table> <tbody><tr> <td class="votecell"> <div class="vote"> <input type="hidden" value="9439806"> <a class="vote-up-off" title="This answer is useful">up vote</a> <span class="vote-count-post ">24</span> <a class="vote-down-off" title="This answer is not useful">down vote</a> <span class="vote-accepted-on load-accepted-answer-date" title="load ing when this answer was accepted...">accepted</span> </div> </td>

<td class="answercell"> <div class="post-text"><p>Start by thinking about the range of the problem:< /p> <blockquote> <p>Given two non-negative integers N and M, returns a remainder of F(N^M) modu lo 10,000,103. N and M are integers within the range [0..10,000,000].</p> </blockquote> <p>So max, that is F(10<sup>100000000000000</sup>). Your solution is to (1) do t hat many additions, which will take some time, and (2) store the result, which i s far, far larger, in a double, that has a maximum range of about 10<sup>308</su p> and is only accurate to 15 significant digits anyways.</p> <p>You need to come up with a better solution. <strong>Start by learning about m odular arithmetic.</strong> Here's a hint: Suppose you want to compute 10<sup>1 00000000000000</sup> mod 9. I'll tell you right now: it's one. <strong>How did I do that so fast?</strong> Because if a mod b = c and d mod b = f then a*d mod b is congruent to c*f. And similarly, a+d mod b is congruent to c+f. These sor ts of facts are crucial when solving these kinds of problems.</p> <p>Once you've got that, then look into <strong>what are the properties of congr uences that involve primes</strong>? They told you that the modulus was a prime number for a reason; you're going to need that fact.</p> <p>A few more thoughts:</p> <ul> <li><p>When faced with these sorts of programming challenge problems, <strong>us e meta reasoning</strong> about the problem itself. You were given the problem " compute fib(a perfect power) modulo a given prime". You decided to try to solve the more general problem of "compute any_recurrance(something) and then compute something modulo something", which is a far, far harder problem. Don't make it h arder on yourself. It is not an accident that the problem is restricted to the f ib recurrance on perfect powers modulo a prime; if you are not using <em>all</em > of those facts in your solution, you're probably doing it wrong.</p></li>

<li><p>The learn-it-yourself-the-hard-way approach: For example, try solving the problem "compute fib(n) modulo 2 for any n". <strong>Solve it by hand.</strong> You should be able to do that with pencil and paper in a few minutes. Then try solving "compute fib(3<sup>m</sup>) modulo 2". Once you know the solution to th e first problem, that should be easy. Then try "compute fib(n<sup>m</sup>) modu lo 2". Again, that should be straightforward given the solution to the previous problem. Once you've solved the problem completely for a modulus of two, try it with a modulus of three. Then five. Then seven. Then eleven. Then for an arbitra ry prime number. And then you will have an algorithm that you can actually turn into code.</p></li> <li><p>The stand-on-the-shoulders-of-others approach: there is an efficient algo rithm (that does not use floating point) for computing fib numbers as "powers" o f a tuple with a special multiplication rule. However, when doing that the numbe rs in the tuple rapidly get <em>huge</em>. You can modify the power rule using t he rules of modular arithmetic to keep the numbers small while still producing t he right answer quickly.</p></li> </ul> </div> <table class="fw"> <tbody><tr> <td class="vt">

<div class="post-menu"><a href="http://stackoverflow.com/a/9439806" title="short permalink to this answer" class="short-link" id="link-post-9439806">share</a><s pan class="lsep">|</span><a href="http://stackoverflow.com/posts/9439806/edit" c lass="suggest-edit-post" title="">improve this answer</a></div> </td> <td align="right" class="post-signature"> <div class="user-info "> <div class="user-action-time"> <a href="http://stackoverflow.com/posts/9439806/revi sions" title="show all edits to this post">edited <span title="2012-02-25 15:23: 24Z" class="relativetime">Feb 25 '12 at 15:23</span></a> </div> <div class="user-gravatar32"> </div> <div class="user-details"> <br> </div> </div> </td>

<td align="right" class="post-signature">

<div class="user-info user-hover"> <div class="user-action-time"> answered <span title="2012-02-25 00:03:1 4Z" class="relativetime">Feb 25 '12 at 0:03</span> </div> <div class="user-gravatar32"> <a href="http://stackoverflow.com/users/88656/eric-lippert"><div cla ss=""><img src="./c# - Codility Fibonacci Solution - Stack Overflow_files/6fbdff 3ffb6f111d172759b4f05bea0e" alt="" width="32" height="32"></div></a> </div> <div class="user-details"> <a href="http://stackoverflow.com/users/88656/eric-lippert">Eric Lip pert</a><br> <span class="reputation-score" title="reputation score 233115" dir=" ltr">233k</span><span title="51 gold badges"><span class="badge1"></span><span c lass="badgecount">51</span></span><span title="514 silver badges"><span class="b adge2"></span><span class="badgecount">514</span></span><span title="1290 bronze badges"><span class="badge3"></span><span class="badgecount">1290</span></span> </div> </div> </td> </tr> </tbody></table> </td> </tr>

<tr> <td class="votecell"></td> <td> <div id="comments-9439806" class="comments"> <table> <tbody> <tr id="comment-11938660" class="comment"> <td></td> <td class="comment-text"><div><span class="comment-copy">very nice answe r.</span> &nbsp;<a href="http://stackoverflow.com/users/416926/saeed-amiri" title ="15569 reputation" class="comment-user">Saeed Amiri</a> <span class="comment-da te" dir="ltr"><a class="comment-link" href="http://stackoverflow.com/questions/9 439352/codility-fibonacci-solution/9439806#comment11938660_9439806"><span title= "2012-02-25 00:25:58Z" class="relativetime-clean">Feb 25 '12 at 0:25</span></a>< /span></div></td> </tr> <tr id="comment-11945435" class="comment"> <td></td> <td class="comment-text"><div><span class="comment-copy">Great answer th anks! Relieved to say that the two questions I had to do before my job intervie w were nowhere near as hard as this one and I scored 100% - still haven't got th is one sussed but I'll get there!</span> &nbsp;<a href="http://stackoverflow.com/ users/843437/kev847382" title="26 reputation" class="comment-user owner">kev8473 82</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="http:/ /stackoverflow.com/questions/9439352/codility-fibonacci-solution/9439806#comment 11945435_9439806"><span title="2012-02-25 14:40:23Z" class="relativetime-clean">

Feb 25 '12 at 14:40</span></a></span></div></td> </tr> <tr id="comment-11945851" class="comment"> <td></td> <td class="comment-text"><div><span class="comment-copy">@kev847382: You 're welcome. This is quite a tricky problem. I cogitated on it a bit and worked out two algorithms; both require some knowledge of (1) how to calculate fib quic kly, much more quickly than doing all the additions, and (2) how to use modular arithmetic correctly and safely to keep the numbers small enough so that every s ub-calculation fits into a long. Good luck!</span> &nbsp;<a href="http://stackove rflow.com/users/88656/eric-lippert" title="233115 reputation" class="comment-use r">Eric Lippert</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="http://stackoverflow.com/questions/9439352/codility-fibonacci-solution/94 39806#comment11945851_9439806"><span title="2012-02-25 15:16:57Z" class="relativ etime-clean">Feb 25 '12 at 15:16</span></a></span></div></td> </tr> <tr id="comment-11946524" class="comment"> <td></td> <td class="comment-text"><div><span class="comment-copy">This explains a lot <a href="http://blog.codility.com/2012/02/its-not-easy-to-get-codility.html " rel="nofollow">blog.codility.com/2012/02/its-not-easy-to-get-codility.html</a> Only 6% of people actually pass these tests and only 1% get Gold. Suddenly I d on't feel so bad.</span> &nbsp;<a href="http://stackoverflow.com/users/843437/kev 847382" title="26 reputation" class="comment-user owner">kev847382</a> <span cla ss="comment-date" dir="ltr"><a class="comment-link" href="http://stackoverflow.c om/questions/9439352/codility-fibonacci-solution/9439806#comment11946524_9439806 "><span title="2012-02-25 16:12:03Z" class="relativetime-clean">Feb 25 '12 at 16 :12</span></a></span><span class="edited-yes" title="this comment was edited"></ span></div></td> </tr> </tbody> </table> </div> </td> </tr> </tbody></table> </div> <div class="everyonelovesstackoverflow adzerk-vote" id="adzerk3"><a href="http://engine.adzerk.net/r?e=eyJhdiI6NDE0LCJhdCI6NCwiY20iOjI5MTEsImNoIjoxM Tc4LCJjciI6NTkxNiwiZGkiOiI2YTNlNWVlOTUyMGU0NTRkYmJjYzQ3M2I2N2RmNTMyNSIsImRtIjoxL CJmYyI6MTAxODEsImZsIjo3NDcyLCJrdyI6ImMjLGZpYm9uYWNjaSxjb2RpbGl0eSIsIm53IjoyMiwic mYiOiJodHRwOi8vc3RhY2tvdmVyZmxvdy5jb20vdGFncy9jb2RpbGl0eS9ob3QiLCJydiI6MCwicHIiO jE2MDQsInN0Ijo4Mjc3LCJ6biI6NDQsInVyIjoiaHR0cDovL2NhcmVlcnMuc3RhY2tvdmVyZmxvdy5jb 20vIn0&s=H8F0WV5mIra9mH2cTyUrMRI_6bU" rel="nofollow" target="_blank" title=""><i mg src="./c# - Codility Fibonacci Solution - Stack Overflow_files/d31c8d7d3cf640 a6b29d9ad71788dd4a.png" title="" alt="" border="0" width="728" height="90"></a>< div class="adzerk-vote-controls" style="display:none;"><div class="adzerk-vote-o ption adzerk-vote-up"><div class="adzerk-vote-icon"></div></div><div class="adze rk-vote-option adzerk-vote-down"><div class="adzerk-vote-icon"></div></div></div ><div class="adzerk-vote-survey" style="display:none;"><form><span>No problem. W e won't show you that ad again. Why didn't you like it?</span><ul><li><label><in put type="radio" value="12" name="downvoteReason">Uninteresting</label></li><li> <label><input type="radio" value="13" name="downvoteReason">Misleading</label></ li><li><label><input type="radio" value="14" name="downvoteReason">Offensive</la bel></li><li><label><input type="radio" value="15" name="downvoteReason">Repetit

ive</label></li></ul><a href="http://stackoverflow.com/questions/9439352/codilit y-fibonacci-solution/9439806#" class="adzerk-vote-cancel">Oops! I didn't mean to do this.</a></form></div><img height="0px" width="0px" border="0" src="./c# - C odility Fibonacci Solution - Stack Overflow_files/i(1).gif"></div>

<a name="9559311"></a> <div id="answer-9559311" class="answer" data-answerid="9559311"> <table> <tbody><tr> <td class="votecell"> <div class="vote"> <input type="hidden" value="9559311"> <a class="vote-up-off" title="This answer is useful">up vote</a> <span class="vote-count-post ">1</span> <a class="vote-down-off" title="This answer is not useful">down vote</a> </div> </td>

<td class="answercell"> <div class="post-text"><p>Below is my code, that seems to work on all my tes ts, but the Codility.com says it do not pass their tests. It has O(log(max(N,M))) time complexity and O(1) memory use.</p> <pre class="lang-cs prettyprint prettyprinted" style=""><code><span class="pln"> </span><span class="kwd">typedef</span><span class="pln"> </span><span class ="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><sp an class="pln"> </span><span class="kwd">long</span><span class="pln"> tlong</sp an><span class="pun">;</span><span class="pln"> </span><span class="com">#ifndef</span><span class="pln"> swap </span><span class="com">#define</span><span class="pln"> swap</span><span c lass="pun">(</span><span class="pln">a</span><span class="pun">,</span><span cla ss="pln">b</span><span class="pun">)</span><span class="pln"> </span><span class ="pun">{</span><span class="pln"> \ </span><span class="pun">(</span><span class="pln">a</span><span class=" pun">)</span><span class="pln"> </span><span class="pun">^=</span><span class="p ln"> </span><span class="pun">(</span><span class="pln">b</span><span class="pun ">);</span><span class="pln"> \ </span><span class="pun">(</span><span class="pln">b</span><span class=" pun">)</span><span class="pln"> </span><span class="pun">^=</span><span class="p ln"> </span><span class="pun">(</span><span class="pln">a</span><span class="pun ">);</span><span class="pln"> \ </span><span class="pun">(</span><span class="pln">a</span><span class=" pun">)</span><span class="pln"> </span><span class="pun">^=</span><span class="p ln"> </span><span class="pun">(</span><span class="pln">b</span><span class="pun ">);</span><span class="pln"> \ </span><span class="pun">};</span><span class="pln"> </span><span class="com">#endif</span><span class="pln"> </span><span class= "com">// !1</span><span class="pln">

</span><span class="kwd">unsigned</span><span class="pln"> </span><span clas s="typ">int</span><span class="pln"> </span><span class="typ">FastLn</span><span class="pun">(</span><span class="kwd">unsigned</span><span class="pln"> </span> <span class="typ">int</span><span class="pln"> n</span><span class="pun">){</spa n><span class="pln"> </span><span class="kwd">unsigned</span><span class="pln"> k</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">for</span><span class="pln"> </span><span class ="pun">(</span><span class="pln">k </span><span class="pun">=</span><span class= "pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="p ln"> n </span><span class="pun">&gt;=</span><span class="lit">2</span><span clas s="pun">;</span><span class="pln"> k</span><span class="pun">++)</span><span cla ss="pln"> </span><span class="pun">{</span><span class="pln"> n </span><span class="pun">&gt;&gt;=</span><span class="pln"> </span ><span class="lit">1</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">return</span><span class="pln"> k</span><span c lass="pun">;</span><span class="pln"> </span><span class="pun">};</span><span class="pln"> </span><span class="kwd">unsigned</span><span class="pln"> </span><span clas s="typ">int</span><span class="pln"> </span><span class="typ">Reduce</span><span class="pun">(</span><span class="kwd">unsigned</span><span class="pln"> </span> <span class="typ">int</span><span class="pln"> N</span><span class="pun">,</span ><span class="pln"> </span><span class="kwd">unsigned</span><span class="pln"> < /span><span class="typ">int</span><span class="pln"> M</span><span class="pun">) {</span><span class="pln"> </span><span class="kwd">const</span><span class="pln"> </span><span cla ss="kwd">unsigned</span><span class="pln"> </span><span class="typ">int</span><s pan class="pln"> mod </span><span class="pun">=</span><span class="pln"> </span> <span class="lit">10000103</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">const</span><span class="pln"> </span><span cla ss="kwd">unsigned</span><span class="pln"> </span><span class="typ">int</span><s pan class="pln"> rep </span><span class="pun">=</span><span class="pln"> </span> <span class="lit">2</span><span class="pun">*</span><span class="pln">mod </span ><span class="pun">+</span><span class="pln"> </span><span class="lit">2</span>< span class="pun">;</span><span class="pln"> </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="typ">int</span><span class="pln"> MM </span><span class="pun">=</span><sp an class="pln"> M</span><span class="pun">;</span><span class="pln"> tlong NN</span><span class="pun">,</span><span class="pln">TOT</span><sp an class="pun">;</span><span class="pln"> NN </span><span class="pun">=</span><span class="pln"> N </span><span cl ass="pun">%</span><span class="pln"> rep</span><span class="pun">;</span><span c lass="pln"> TOT </span><span class="pun">=</span><span class="pln"> </span><span cla ss="lit">1</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">while</span><span class="pun">(</span><span cla ss="pln">MM </span><span class="pun">&gt;=</span><span class="pln"> </span><span class="lit">2</span><span class="pun">){</span><span class="pln"> </span><span class="kwd">if</span><span class="pun">(</span><span cl ass="pln">MM</span><span class="pun">%</span><span class="lit">2</span><span cla ss="pun">){</span><span class="pln"> TOT </span><span class="pun">*=</span><span class="pln"> </span> <span class="pun">(</span><span class="pln">NN</span><span class="pun">);</span> <span class="pln">TOT </span><span class="pun">%=</span><span class="pln"> rep</ span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> NN </span><span class="pun">*=</span><span class="pln"> NN</span><sp an class="pun">;</span><span class="pln"> NN </span><span class="pun">%=</span><

span class="pln"> rep</span><span class="pun">;</span><span class="pln"> MM </span><span class="pun">/=</span><span class="pln"> </span><span class="lit">2</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> NN </span><span class="pun">*=</span><span class="pln"> TOT</span><span class="pun">;</span><span class="pln"> NN </span><span class="pun">%=</span><spa n class="pln"> rep</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">return</span><span class="pln"> </span><span cl ass="pun">(</span><span class="kwd">unsigned</span><span class="pln"> </span><sp an class="typ">int</span><span class="pun">)(</span><span class="pln">NN</span>< span class="pun">);</span><span class="pln"> </span><span class="pun">};</span><span class="pln"> </span><span class="kwd">unsigned</span><span class="pln"> </span><span clas s="typ">int</span><span class="pln"> </span><span class="typ">Fn</span><span cla ss="pun">(</span><span class="kwd">unsigned</span><span class="pln"> </span><spa n class="typ">int</span><span class="pln"> N</span><span class="pun">){</span><s pan class="pln"> </span><span class="kwd">switch</span><span class="pln"> </span><span cl ass="pun">(</span><span class="pln">N</span><span class="pun">){</span><span cla ss="pln"> </span><span class="kwd">case</span><span class="pun">(</span><span class="lit">0</span><span class="pun">):</span><span class="kwd">return</span><s pan class="pln"> </span><span class="lit">0</span><span class="pun">;</span><spa n class="pln"> </span><span class="kwd">case</span><span class="pun">(</span><span class="lit">1</span><span class="pun">):</span><span class="kwd">return</span><s pan class="pln"> </span><span class="lit">1</span><span class="pun">;</span><spa n class="pln"> </span><span class="kwd">case</span><span class="pun">(</span><span class="lit">2</span><span class="pun">):</span><span class="kwd">return</span><s pan class="pln"> </span><span class="lit">1</span><span class="pun">;</span><spa n class="pln"> </span><span class="kwd">case</span><span class="pun">(</span><span class="lit">3</span><span class="pun">):</span><span class="kwd">return</span><s pan class="pln"> </span><span class="lit">2</span><span class="pun">;</span><spa n class="pln"> </span><span class="kwd">case</span><span class="pun">(</span><span class="lit">4</span><span class="pun">):</span><span class="kwd">return</span><s pan class="pln"> </span><span class="lit">3</span><span class="pun">;</span><spa n class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">const</span><span class="pln"> </span><span cla ss="kwd">unsigned</span><span class="pln"> </span><span class="typ">int</span><s pan class="pln"> mod </span><span class="pun">=</span><span class="pln"> </span> <span class="lit">10000103</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="typ">int</span><span class="pln"> M</span><span class="pun">,</span><span class="pln">l</span><span class="pun">,</span><span class="pln">a</span><span c lass="pun">,</span><span class="pln">b</span><span class="pun">,</span><span cla ss="pln">n2</span><span class="pun">;</span><span class="pln"> tlong ta</span><span class="pun">,</span><span class="pln">tb</span><spa n class="pun">;</span><span class="pln"> M </span><span class="pun">=</span><span class="pln"> </span><span class ="lit">2</span><span class="pun">;</span><span class="pln"> a </span><span class="pun">=</span><span class="pln"> </span><span class ="lit">1</span><span class="pun">;</span><span class="pln">b </span><span class= "pun">=</span><span class="pln"> </span><span class="lit">1</span><span class="p un">;</span><span class="pln"> l </span><span class="pun">=</span><span class="pln"> </span><span class ="typ">FastLn</span><span class="pun">(</span><span class="pln">N</span><span cl

ass="pun">);</span><span class="pln"> n2 </span><span class="pun">=</span><span class="pln"> </span><span clas s="lit">1</span><span class="pln"> </span><span class="pun">&lt;&lt;</span><span class="pln"> </span><span class="pun">(</span><span class="pln">l</span><span c lass="pun">-</span><span class="lit">1</span><span class="pun">);</span><span cl ass="pln"> </span><span class="kwd">while</span><span class="pun">(</span><span cla ss="pln">M </span><span class="pun">&lt;</span><span class="pln"> N</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span cl ass="pun">((</span><span class="pln">M</span><span class="pun">+</span><span cla ss="lit">1</span><span class="pun">)</span><span class="pln"> </span><span class ="pun">*</span><span class="pln"> n2 </span><span class="pun">&lt;=</span><span class="pln"> N</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> swap</span><span class="pun">(</span><span class="pln">a</span>< span class="pun">,</span><span class="pln">b</span><span class="pun">);</span><s pan class="pln"> a </span><span class="pun">+=</span><span class="pln"> b</span>< span class="pun">;</span><span class="pln"> a </span><span class="pun">%=</span><span class="pln"> mod</span ><span class="pun">;</span><span class="pln"> M</span><span class="pun">++;</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">if</span><span class="pun">(</span><span cl ass="pln">M </span><span class="pun">==</span><span class="pln"> N</span><span c lass="pun">){</span><span class="pln"> </span><span class="kwd">break</span><span class="pun">;</span>< span class="pln"> </span><span class="pun">};</span><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span cl ass="pun">(</span><span class="pln">M</span><span class="pun">&gt;</span><span c lass="pln">N</span><span class="pun">)</span><span class="pln"> M</span><span cl ass="pun">/=</span><span class="lit">0</span><span class="pun">;</span><span cla ss="com">//A kind of assert, that should throw a DivisionByZero exception, to as sure we don't have computational problems</span><span class="pln"> ta </span><span class="pun">=</span><span class="pln"> a</span><span class="pun">;</span><span class="pln"> ta </span><span class="pun">*=</span><span class="pln"> ta</span><sp an class="pun">;</span><span class="pln"> </span><span class="com">//ta %= mod;</span><span class="pln"> tb </span><span class="pun">=</span><span class="pln"> b</span><span class="pun">;</span><span class="pln"> tb </span><span class="pun">*=</span><span class="pln"> tb</span><sp an class="pun">;</span><span class="pln"> tb </span><span class="pun">+=</span><span class="pln"> ta</span><sp an class="pun">;</span><span class="pln"> tb </span><span class="pun">%=</span><span class="pln"> mod</span><s pan class="pun">;</span><span class="pln"> ta </span><span class="pun">+=</span><span class="pln"> </span><span class="lit">2</span><span class="pln"> </span><span class="pun">*</span><span c lass="pln"> </span><span class="pun">(</span><span class="pln">tlong</span><span class="pun">)(</span><span class="pln">a</span><span class="pun">)</span><span class="pln"> </span><span class="pun">*</span><span class="pln"> </span><span cl ass="pun">(</span><span class="pln">tlong</span><span class="pun">)(</span><span class="pln">b</span><span class="pun">);</span><span class="pln"> ta </span><span class="pun">%=</span><span class="pln"> mod</span><s pan class="pun">;</span><span class="pln"> b </span><span class="pun">=</span><span class="pln"> tb</span><span

class="pun">;</span><span class="pln"> a </span><span class="pun">=</span><span class="pln"> ta</span><span class="pun">;</span><span class="com">//Using F(2*n) = F(N)*(F(N) + 2*F(N-1)) i dentity</span><span class="pln"> M </span><span class="pun">&lt;&lt;=</span><span class="pln"> </span ><span class="lit">1</span><span class="pun">;</span><span class="pln"> n2 </span><span class="pun">&gt;&gt;=</span><span class="pln"> </spa n><span class="lit">1</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">return</span><span class="pln"> a</span><span c lass="pun">;</span><span class="pln"> </span><span class="pun">};</span><span class="pln"> </span><span class="typ">int</span><span class="pln"> power_fib </span><span class="pun">(</span><span class="pln"> </span><span class="typ">int</span><span class="pln"> N</span><span class="pun">,</span><span class="typ">int</span><spa n class="pln"> M </span><span class="pun">)</span><span class="pln"> </span><spa n class="pun">{</span><span class="pln"> </span><span class="kwd">unsigned</span><span class="pln"> a</span><span class="pun">,</span><span class="pln">b</span><span class="pun">;</span><span c lass="pln"> </span><span class="kwd">const</span><span class="pln"> </span><span cla ss="kwd">unsigned</span><span class="pln"> </span><span class="typ">int</span><s pan class="pln"> mod </span><span class="pun">=</span><span class="pln"> </span> <span class="lit">10000103</span><span class="pun">;</span><span class="pln"> a </span><span class="pun">=</span><span class="pln"> </span><span class ="lit">1</span><span class="pun">;</span><span class="pln"> b </span><span class="pun">=</span><span class="pln"> </span><span class ="lit">1</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">if</span><span class="pun">(</span><span class= "pln">N </span><span class="pun">==</span><span class="pln"> </span><span class= "lit">0</span><span class="pun">)</span><span class="kwd">return</span><span cla ss="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class ="pln"> </span><span class="kwd">if</span><span class="pun">(</span><span class= "pln">N </span><span class="pun">==</span><span class="pln"> </span><span class= "lit">1</span><span class="pun">)</span><span class="kwd">return</span><span cla ss="pln"> </span><span class="lit">1</span><span class="pun">;</span><span class ="pln"> </span><span class="kwd">if</span><span class="pun">(</span><span class= "pln">M </span><span class="pun">==</span><span class="pln"> </span><span class= "lit">0</span><span class="pun">)</span><span class="kwd">return</span><span cla ss="pln"> </span><span class="lit">1</span><span class="pun">;</span><span class ="pln"> </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="typ">int</span><span class="pln"> NN</span><span class="pun">;</span><spa n class="pln"> NN </span><span class="pun">=</span><span class="pln"> </span><span clas s="typ">Reduce</span><span class="pun">((</span><span class="kwd">unsigned</span ><span class="pln"> </span><span class="typ">int</span><span class="pun">)</span ><span class="pln">N</span><span class="pun">,(</span><span class="kwd">unsigned </span><span class="pln"> </span><span class="typ">int</span><span class="pun">) </span><span class="pln">M</span><span class="pun">);</span><span class="pln"> </span><span class="kwd">return</span><span class="pln"> </span><span cl ass="pun">(</span><span class="typ">int</span><span class="pun">)</span><span cl ass="typ">Fn</span><span class="pun">((</span><span class="kwd">unsigned</span>< span class="pln"> </span><span class="typ">int</span><span class="pun">)</span>< span class="pln">NN</span><span class="pun">);</span><span class="pln"> </span><span class="pun">}</span></code></pre>

<p>What do you think could be wrong with this solution?</p> </div> <table class="fw"> <tbody><tr> <td class="vt">

<div class="post-menu"><a href="http://stackoverflow.com/a/9559311" title="short permalink to this answer" class="short-link" id="link-post-9559311">share</a><s pan class="lsep">|</span><a href="http://stackoverflow.com/posts/9559311/edit" c lass="suggest-edit-post" title="">improve this answer</a></div> </td>

<td align="right" class="post-signature">

<div class="user-info "> <div class="user-action-time"> answered <span title="2012-03-04 22:13:2 3Z" class="relativetime">Mar 4 '12 at 22:13</span> </div> <div class="user-gravatar32"> <a href="http://stackoverflow.com/users/691343/asu"><div class=""><i mg src="./c# - Codility Fibonacci Solution - Stack Overflow_files/195087f49b0cb3 3b7bf4b35b32b7c0f0" alt="" width="32" height="32"></div></a> </div> <div class="user-details"> <a href="http://stackoverflow.com/users/691343/asu">asu</a><br> <span class="reputation-score" title="reputation score" dir="ltr">16 </span><span title="3 bronze badges"><span class="badge3"></span><span class="ba dgecount">3</span></span> </div> </div> </td> </tr> </tbody></table> </td> </tr>

<tr> <td class="votecell"></td> <td> <div id="comments-9559311" class="comments dno"> <table>

<tbody> <tr><td></td><td></td></tr> </tbody> </table> </div> </td> </tr> </tbody></table> </div> <a name="9439574"></a> <div id="answer-9439574" class="answer" data-answerid="9439574"> <table> <tbody><tr> <td class="votecell"> <div class="vote"> <input type="hidden" value="9439574"> <a class="vote-up-off" title="This answer is useful">up vote</a> <span class="vote-count-post ">-1</span> <a class="vote-down-off" title="This answer is not useful">down vote</a> </div> </td>

<td class="answercell"> <div class="post-text"><p>It looks like you're overflowing the int value for the return type of ModFib and for the modfib variable. Try using a long instea d.</p> </div> <table class="fw"> <tbody><tr> <td class="vt">

<div class="post-menu"><a href="http://stackoverflow.com/a/9439574" title="short permalink to this answer" class="short-link" id="link-post-9439574">share</a><s pan class="lsep">|</span><a href="http://stackoverflow.com/posts/9439574/edit" c lass="suggest-edit-post" title="">improve this answer</a></div> </td>

<td align="right" class="post-signature">

<div class="user-info "> <div class="user-action-time"> answered <span title="2012-02-24 23:34:5 7Z" class="relativetime">Feb 24 '12 at 23:34</span> </div> <div class="user-gravatar32"> <a href="http://stackoverflow.com/users/223970/natethegreat"><div cl ass=""><img src="./c# - Codility Fibonacci Solution - Stack Overflow_files/1d4ec da86a8b62df15663b961b83a423" alt="" width="32" height="32"></div></a> </div> <div class="user-details"> <a href="http://stackoverflow.com/users/223970/natethegreat">NateThe Great</a><br> <span class="reputation-score" title="reputation score" dir="ltr">1, 387</span><span title="4 silver badges"><span class="badge2"></span><span class= "badgecount">4</span></span><span title="6 bronze badges"><span class="badge3">< /span><span class="badgecount">6</span></span> </div> </div> </td> </tr> </tbody></table> </td> </tr>

<tr> <td class="votecell"></td> <td> <div id="comments-9439574" class="comments"> <table> <tbody> <tr id="comment-11938053" class="comment"> <td></td> <td class="comment-text"><div><span class="comment-copy">Well the soluti on requires that power_fib returns an int.... x mod 10000103 should be a valid i nt</span> &nbsp;<a href="http://stackoverflow.com/users/843437/kev847382" title=" 26 reputation" class="comment-user owner">kev847382</a> <span class="comment-dat e" dir="ltr"><a class="comment-link" href="http://stackoverflow.com/questions/94 39352/codility-fibonacci-solution/9439806#comment11938053_9439574"><span title=" 2012-02-24 23:41:05Z" class="relativetime-clean">Feb 24 '12 at 23:41</span></a>< /span><span class="edited-yes" title="this comment was edited"></span></div></td > </tr> </tbody> </table> </div> </td>

</tr> </tbody></table> </div> <a name="new-answer"></a> <form id="post-form" action="http://stackoverflow.com/questions/9439352/answ er/submit" method="post" class="post-form"> <input type="hidden" id="post-id" value="9439352"> <h2 class="space">Your Answer</h2>

<script type="text/javascript"> StackExchange.ready(function() { initTagRenderer("".split(" "), "".split(" ")); prepareEditor({ heartbeatType: 'answer', bindNavPrevention: true, postfix: "", onDemand: true, discardSelector: ".discard-answer" ,immediatelyShowMarkdownHelp:true }); }); </script> <div id="post-editor" class="post-editor"> <div style="position: relative;"> <div class="wmd-container"> <div id="wmd-button-bar" class="wmd-button-bar"><ul id="wmd-button-r ow" class="wmd-button-row"><li id="wmd-bold-button" class="wmd-button" style="le ft: 0px;"><span style="background-position: 0px -20px;"></span></li><li id="wmditalic-button" class="wmd-button" style="left: 25px;"><span style="background-po sition: -20px -20px;"></span></li><li id="wmd-spacer1" class="wmd-spacer" style= "left: 50px;"><span style="background-position: -40px -20px;"></span></li><li id ="wmd-link-button" class="wmd-button" style="left: 75px;"><span style="backgroun d-position: -40px -20px;"></span></li><li id="wmd-quote-button" class="wmd-butto n" style="left: 100px;"><span style="background-position: -60px -20px;"></span>< /li><li id="wmd-code-button" class="wmd-button" style="left: 125px;"><span style ="background-position: -80px -20px;"></span></li><li id="wmd-image-button" class ="wmd-button" style="left: 150px;"><span style="background-position: -100px -20p x;"></span></li><li id="wmd-spacer2" class="wmd-spacer" style="left: 175px;"><sp an style="background-position: -120px -20px;"></span></li><li id="wmd-olist-butt on" class="wmd-button" style="left: 200px;"><span style="background-position: -1 20px -20px;"></span></li><li id="wmd-ulist-button" class="wmd-button" style="lef t: 225px;"><span style="background-position: -140px -20px;"></span></li><li id=" wmd-heading-button" class="wmd-button" style="left: 250px;"><span style="backgro und-position: -160px -20px;"></span></li><li id="wmd-hr-button" class="wmd-butto n" style="left: 275px;"><span style="background-position: -180px -20px;"></span> </li><li id="wmd-spacer3" class="wmd-spacer" style="left: 300px;"><span style="b ackground-position: -200px -20px;"></span></li><li id="wmd-undo-button" class="w md-button" style="left: 325px;"><span style="background-position: -200px -20px;" ></span></li><li id="wmd-redo-button" class="wmd-button" style="left: 350px;"><s pan style="background-position: -220px -20px;"></span></li></ul></div> <textarea id="wmd-input" class="wmd-input" name="post-text" cols="92

" rows="15" tabindex="101" data-min-length=""></textarea> </div> </div> <div class="fl" style="margin-top: 8px; height:24px;">&nbsp;</div> <div id="draft-saved" class="draft-saved community-option fl" style="margintop: 8px; height:24px; display:none;">draft saved</div> <div id="draft-discarded" class="draft-discarded community-option fl" style= "margin-top: 8px; height:24px; display:none;">draft discarded</div>

<div id="wmd-preview" class="wmd-preview"></div> <div></div> <div class="edit-block"> <input id="fkey" name="fkey" type="hidden" value="9336b12e86af343415687d 9e813eec5f"> <input id="author" name="author" type="text"> </div> </div> <div style="position: relative;"> <div class="form-item new-post-login"> <div class="new-login-form"> <div class="new-login-left"> <h3>Sign up or <a id="login-link" href="http://stackover flow.com/users/login?returnurl=%2fquestions%2f9439352%2fcodility-fibonacci-solut ion%23new-answer">log in</a></h3> <script type="text/javascript"> StackExchange.ready(function () { StackExchange.helpers.onClickDraftSave('#login-l ink'); }); </script> <div class="preferred-login google-login"> <p><span class="icon"></span><span>Sign up using Goo gle</span></p> </div> <div class="preferred-login facebook-login"> <p><span class="icon"></span><span>Sign up using Fac ebook</span></p> </div> <div class="preferred-login stackexchange-login"> <p><span class="icon"></span><span>Sign up using Sta ck Exchange</span></p> </div> </div> <input type="hidden" name="manual-openid" class="manual-open id"> <input type="hidden" name="use-facebook" class="use-facebook " value="false"> <input type="button" class="submit-openid" value="Submit" st yle="display:none"> <div class="new-login-right"> <h3>Post as a guest</h3> <div class="form-item"> <table> <tbody><tr> <script type="text/javascript">

StackExchange.ready(function () { StackExchange.helpers.bindHelpOverlayEvents($('.vm input')); }); </script> <td class="vm"> <div> <label for="display-name">Name</label> <input id="display-name" name="display-name" type="text" siz e="30" maxlength="30" value="" tabindex="105"> </div> <div> <label for="m-address">Email</label> <input id="m-address" name="m-address" type="text" size="30" maxlength="100" value="" tabindex="106"> <span class="edit-field-overlay">required, but not shown</sp an> </div> </td> </tr> </tbody></table> </div> </div> </div> </div> <script type="text/javascript"> StackExchange.ready( function () { StackExchange.openid.initPostLogin('.new-post-login', '% 2fquestions%2f9439352%2fcodility-fibonacci-solution%23new-answer'); } ); </script> <noscript> &lt;h3&gt;Post as a guest&lt;/h3&gt; &lt;div class="form-item"&gt; &lt;table&gt; &lt;tr&gt; &lt;script type="text/javascript"&gt; StackExchange.ready(function () { StackExchange.helpers.bindHelpOverlayEvents($('.vm input')); }); &lt;/script&gt; &lt;td class="vm"&gt; &lt;div&gt; &lt;label for="display-name"&gt;Name&lt;/label&gt; &lt;input id="display-name" name="display-name" type="text" size="30" maxlength="30" value="" tabindex="105"&gt; &lt;/div&gt; &lt;div&gt; &lt;label for="m-address"&gt;Email&lt;/label&gt; &lt;input id="m-address" name="m-address" type="text" size=" 30" maxlength="100" value="" tabindex="106"&gt; &lt;span class="edit-field-overlay"&gt;required, but not sho wn&lt;/span&gt; &lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt;

</noscript> </div> <div class="form-submit cbt"> <input id="submit-button" type="submit" value="Post Your Answer" tab index="110"> <a href="http://stackoverflow.com/questions/9439352/codility-fibonac ci-solution/9439806#" class="discard-answer dno">discard</a> <p class="privacy-policy-agreement"> By posting your answer, you agree to the <a href="http://stackexchange.com/legal/privacy-policy" target="_blank"> privacy policy</a> and <a href="http://stackexchange.com/legal/terms-of-service" target="_blank ">terms of service</a>. </p> <input type="hidden" name="legalLinksShown" value="1"> </div> </form>

<h2 class="bottom-notice"> Not the answer you're looking for? Browse other questions tagged <a href="http://stackoverflow.com/ques tions/tagged/c%23" class="post-tag" title="show questions tagged &#39;c#&#39;" r el="tag">c#</a> <a href="http://stackoverflow.com/questions/tagged/fibonacci" cl ass="post-tag" title="show questions tagged &#39;fibonacci&#39;" rel="tag">fibon acci</a> <a href="http://stackoverflow.com/questions/tagged/codility" class="pos t-tag" title="show questions tagged &#39;codility&#39;" rel="tag">codility</a> or <a href="http://stackoverflow.com/questions/ask">ask your own question</a>. </h2> </div> </div> <div id="sidebar" class="show-votes"> <div class="module question-stats"> <p class="label-key">tagged</p> <div class="tagged"><a href="http://stackoverflow.com/questions/tagged/c %23" class="post-tag" title="show questions tagged &#39;c#&#39;" rel="tag">c#</a >&nbsp;<span class="item-multiplier"><span class="item-multiplier-x"></span>&nbsp ;<span class="item-multiplier-count">480364</span></span><br> <a href="http://stackoverflow.com/questions/tagged/fibonacci" class="post-tag" t itle="show questions tagged &#39;fibonacci&#39;" rel="tag">fibonacci</a>&nbsp;<s pan class="item-multiplier"><span class="item-multiplier-x"></span>&nbsp;<span cl ass="item-multiplier-count">353</span></span><br> <a href="http://stackoverflow.com/questions/tagged/codility" class="post-tag" ti tle="show questions tagged &#39;codility&#39;" rel="tag">codility</a>&nbsp;<span class="item-multiplier"><span class="item-multiplier-x"></span>&nbsp;<span class ="item-multiplier-count">21</span></span><br> </div> <table id="qinfo"> <tbody><tr> <td><p class="label-key">asked</p></td> <td style="padding-left:10px"><p class="label-key" title="2012-0 2-24 23:09:10Z"><b>1 year ago</b></p></td> </tr> <tr> <td><p class="label-key">viewed</p></td>

<td style="padding-left:10px"> <p class="label-key"> <b>6531 times</b> </p> </td> </tr> <tr> <td><p class="label-key">active</p></td> <td style="padding-left:10px"><p class="label-key"><b><a href="h ttp://stackoverflow.com/questions/9439352/codility-fibonacci-solution/9439806?la stactivity" class="lastactivity-link" title="2012-03-04 22:13:23Z">1 year ago</a ></b></p></td> </tr> </tbody></table> </div> <div class="module community-bulletin" data-tracker="cb=1"> <h4>Community Bulletin</h4> <div class="related"> <div class="spacer"> <div class="bulletin-item-type"><a href="http://blog.stackoverflow.c om/?cb=1" class="event-date" target="_blank">blog</a></div> <div class="bulletin-item-content"> <a href="http://blog.stackoverflow.com/2013/07/say-hi-to-nine-of -our-newest-newbies/?cb=1" class="question-hyperlink" target="_blank">Say Hi to Nine of Our Newest Newbies</a> </div> <br class="cbt"> </div> </div> </div> <script type="text/javascript"> var scriptSrc = "http://engine.adzerk.net/z/8277/adzerk1_2_4 _43,adzerk2_2_17_45,adzerk3_2_4_44?keywords=c%23,fibonacci,codility"; if (document.referrer) { if (/\?/.test(scriptSrc)) scriptSrc += "&"; else scriptSrc += "?"; scriptSrc += "xReferrer=" + document.referrer; } StackExchange.ready(function() { var z = document.createElement("script"); z.type = "text/javascript"; z.async = "true"; z.src = scriptSrc; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(z, s); }); </script> <div class="everyonelovesstackoverflow adzerk-vote" id="adzerk2"><a href="http://engine.adzerk.net/r?e=eyJhdiI6NDE0LCJhdCI6MTcsImNtIjo4NDcsImNoIjoxM Tc4LCJjciI6OTMzNiwiZGkiOiJlMjFkZTQ1NzhhOWQ0YWZmOTAwNTNiNWU1NzE5MDVkYyIsImRtIjoxL CJmYyI6MTQ4NDYsImZsIjoyNDQ0LCJrdyI6ImMjLGZpYm9uYWNjaSxjb2RpbGl0eSIsIm53IjoyMiwic mYiOiJodHRwOi8vc3RhY2tvdmVyZmxvdy5jb20vdGFncy9jb2RpbGl0eS9ob3QiLCJydiI6MCwicHIiO jE1NjgsInN0Ijo4Mjc3LCJ6biI6NDUsInVyIjoiaHR0cDovL2NhcmVlcnMuc3RhY2tvdmVyZmxvdy5jb 20vIn0&s=XXzVlMPFxlWqIsmZKVTkbJfU70o" rel="nofollow" target="_blank" title=""><i mg src="./c# - Codility Fibonacci Solution - Stack Overflow_files/d18eea9d28f349 0b8dcbfa9e38f8336e.jpg" title="" alt="" border="0" width="220" height="250"></a> <div class="adzerk-vote-controls" style="display:none;"><div class="adzerk-vote-

option adzerk-vote-up"><div class="adzerk-vote-icon"></div></div><div class="adz erk-vote-option adzerk-vote-down"><div class="adzerk-vote-icon"></div></div></di v><div class="adzerk-vote-survey" style="display:none;"><form><span>No problem. We won't show you that ad again. Why didn't you like it?</span><ul><li><label><i nput type="radio" value="12" name="downvoteReason">Uninteresting</label></li><li ><label><input type="radio" value="13" name="downvoteReason">Misleading</label>< /li><li><label><input type="radio" value="14" name="downvoteReason">Offensive</l abel></li><li><label><input type="radio" value="15" name="downvoteReason">Repeti tive</label></li></ul><a href="http://stackoverflow.com/questions/9439352/codili ty-fibonacci-solution/9439806#" class="adzerk-vote-cancel">Oops! I didn't mean t o do this.</a></form></div><img height="0px" width="0px" border="0" src="./c# Codility Fibonacci Solution - Stack Overflow_files/i(2).gif"></div> <div id="hireme"> <script type="text/javascript"> window.careers_adurl="http://careers.stackoverflow.com/gethired/js",window.caree rs_cssurl="//cdn-careers.sstatic.net/careers/gethired/sidebar.min.css?v=17be6ca0 db4b",window.careers_leaderboardcssurl="//cdn-careers.sstatic.net/careers/gethir ed/ninja.min.css?v=78ccbfa5c59f",window.careers_companycssurl="//cdn-careers.sst atic.net/careers/gethired/company",window.careers_adselector="div.hireme, div#hi reme",StackExchange.ready(function(){$.ajax({url:"//cdn-careers.sstatic.net/care ers/gethired/loader.min.js?v=607ab2dda910",dataType:"script",cache:!0})}) </script> </div>

<div class="module sidebar-related"> <h4 id="h-related">Related</h4> <div class="related" data-tracker="rq=1"> <div class="spacer"> <a href="http://stackoverflow.com/q/1076001?rq=1" title="Vote score (upvotes - d ownvotes)"> <div class="answer-votes answered-accepted default">1</div> </a> <a href="http://stackoverflow.com/questions/1076001/c-sharp-fibonacci-function-r eturning-errors?rq=1" class="question-hyperlink">C# fibonacci function returning errors</a> </div> <div class="spacer"> <a href="http://stackoverflow.com/q/4703047?rq=1" title="Vote score (upvotes - d ownvotes)"> <div class="answer-votes answered-accepted default">4</div> </a> <a href="http://stackoverflow.com/questions/4703047/review-of-a-codility-test-pa ir-sum-even-count?rq=1" class="question-hyperlink">review of a codility test - p air_sum_even_count</a> </div> <div class="spacer"> <a href="http://stackoverflow.com/q/8623924?rq=1" title="Vote score (upvotes - d ownvotes)"> <div class="answer-votes default">0</div> </a> <a href="http://stackoverflow.com/questions/8623924/dictionary-runtime-codilitytest?rq=1" class="question-hyperlink">Dictionary runtime (Codility Test)</a> </div> <div class="spacer"> <a href="http://stackoverflow.com/q/8624083?rq=1" title="Vote score (upvotes - d ownvotes)">

<div class="answer-votes default">0</div> </a> <a href="http://stackoverflow.com/questions/8624083/dictionary-runtime-codilitytest-repost?rq=1" class="question-hyperlink">Dictionary runtime (Codility Test) Repost</a> </div> <div class="spacer"> <a href="http://stackoverflow.com/q/10146093?rq=1" title="Vote score (upvotes downvotes)"> <div class="answer-votes default">0</div> </a> <a href="http://stackoverflow.com/questions/10146093/codility-extreme-large-numb er-error?rq=1" class="question-hyperlink">Codility extreme large Number error</a > </div> <div class="spacer"> <a href="http://stackoverflow.com/q/12417383?rq=1" title="Vote score (upvotes downvotes)"> <div class="answer-votes answered-accepted default">9</div> </a> <a href="http://stackoverflow.com/questions/12417383/programming-test-codility-d ominator?rq=1" class="question-hyperlink">Programming Test - Codility - Dominato r</a> </div> <div class="spacer"> <a href="http://stackoverflow.com/q/13018278?rq=1" title="Vote score (upvotes downvotes)"> <div class="answer-votes default">4</div> </a> <a href="http://stackoverflow.com/questions/13018278/returning-nth-fibonacci-num ber-the-sequence?rq=1" class="question-hyperlink">Returning Nth Fibonacci number the sequence?</a> </div> <div class="spacer"> <a href="http://stackoverflow.com/q/15490659?rq=1" title="Vote score (upvotes downvotes)"> <div class="answer-votes default">0</div> </a> <a href="http://stackoverflow.com/questions/15490659/concrete-disks-codility-err or?rq=1" class="question-hyperlink">Concrete Disks Codility Error</a> </div> <div class="spacer"> <a href="http://stackoverflow.com/q/16953803?rq=1" title="Vote score (upvotes downvotes)"> <div class="answer-votes default">0</div> </a> <a href="http://stackoverflow.com/questions/16953803/codility-certification-june -2013?rq=1" class="question-hyperlink">codility certification june 2013</a> </div> <div class="spacer"> <a href="http://stackoverflow.com/q/17164164?rq=1" title="Vote score (upvotes downvotes)"> <div class="answer-votes default">0</div> </a> <a href="http://stackoverflow.com/questions/17164164/javascript-codility-demo-so lution?rq=1" class="question-hyperlink">JavaScript Codility Demo Solution</a> </div> </div> </div>

</div> <div id="feed-link"> <div id="feed-link-text"><a href="http://stackoverflow.com/feeds/question/94 39352" title="feed of this question and its answers"><span class="feed-icon"></s pan>question feed</a></div> </div> <script type="text/javascript"> StackExchange.ready(function(){$.get('/posts/9439352/ivc/7ea7');}); </script> <noscript> &lt;div&gt;&lt;img src="/posts/9439352/ivc/7ea7" class="dno" alt="" width="0 " height="0"&gt;&lt;/div&gt; </noscript><div style="display:none" id="prettify-lang">lang-cs</div></div> </div> </div> <div id="footer" class="categories"> <div class="footerwrap"> <div id="footer-menu"> <div class="top-footer-links"> <a href="http://stackoverflow.com/about">about</a> <a href="http://stackoverflow.com/help">help</a> <a href="http://stackoverflow.com/help/badges">badges</a > <a href="http://blog.stackexchange.com/?blb=1">blog</a> <a href="http://chat.stackoverflow.com/">chat</a> <a href="http://data.stackexchange.com/">data</a> <a href="http://stackexchange.com/legal">legal</a> <a href="http://stackexchange.com/legal/privacy-policy">priv acy policy</a> <a href="http://stackexchange.com/about/hiring">jobs</a> <a href="http://engine.adzerk.net/redirect/0/2776/2751/0/4de 3c60f719c4dfcb1a57531c7050090/0">advertising info</a> <a onclick="StackExchange.switchMobile(&quot;on&quot;, &quot ;/questions/9439352/codility-fibonacci-solution/9439806&quot;)">mobile</a> <b><a href="http://stackoverflow.com/contact">contact us</a> </b> <b><a href="http://meta.stackoverflow.com/">feedback</a> </b> </div> <div id="footer-sites"> <table> <tbody><tr> <th colspan="3"> Technology </th> <th> Life / Arts </th> <th> Culture / Recreation </th> <th> Science </th> <th> Other

</th> </tr> <tr> <td> <ol> <li><a href="http://stackoverflow.com/" title="professio nal and enthusiast programmers">Stack Overflow</a></li> <li><a href="http://serverfault.com/" title="professiona l system and network administrators">Server Fault</a></li> <li><a href="http://superuser.com/" title="computer enth usiasts and power users">Super User</a></li> <li><a href="http://webapps.stackexchange.com/" title="p ower users of web applications">Web Applications</a></li> <li><a href="http://askubuntu.com/" title="Ubuntu users and developers">Ask Ubuntu</a></li> <li><a href="http://webmasters.stackexchange.com/" title ="pro webmasters">Webmasters</a></li> <li><a href="http://gamedev.stackexchange.com/" title="p rofessional and independent game developers">Game Development</a></li> <li><a href="http://tex.stackexchange.com/" title="users of TeX, LaTeX, ConTeXt, and related typesetting systems">TeX - LaTeX</a></li> </ol></td><td><ol> <li><a href="http://programmers.stackexchange.com/" titl e="professional programmers interested in conceptual questions about software de velopment">Programmers</a></li> <li><a href="http://unix.stackexchange.com/" title="user s of Linux, FreeBSD and other Un*x-like operating systems.">Unix &amp; Linux</a> </li> <li><a href="http://apple.stackexchange.com/" title="pow er users of Apple hardware and software">Ask Different (Apple)</a></li> <li><a href="http://wordpress.stackexchange.com/" title= "WordPress developers and administrators">WordPress Answers</a></li> <li><a href="http://gis.stackexchange.com/" title="carto graphers, geographers and GIS professionals">Geographic Information Systems</a>< /li> <li><a href="http://electronics.stackexchange.com/" titl e="electronics and electrical engineering professionals, students, and enthusias ts">Electrical Engineering</a></li> <li><a href="http://android.stackexchange.com/" title="e nthusiasts and power users of the Android operating system">Android Enthusiasts< /a></li> <li><a href="http://security.stackexchange.com/" title=" IT security professionals">IT Security</a></li> </ol></td><td><ol> <li><a href="http://dba.stackexchange.com/" title="datab ase professionals who wish to improve their database skills and learn from other s in the community">Database Administrators</a></li> <li><a href="http://drupal.stackexchange.com/" title="Dr upal developers and administrators">Drupal Answers</a></li> <li><a href="http://sharepoint.stackexchange.com/" title ="SharePoint enthusiasts">SharePoint</a></li> <li><a href="http://ux.stackexchange.com/" title="user e xperience researchers and experts">User Experience</a></li> <li><a href="http://mathematica.stackexchange.com/" titl e="users of Mathematica">Mathematica</a></li> <li> <a href="http://stackexchange.com/sites#technology" class="more"> more (13)

</a> </li> </ol> </td> <td> <ol> <li><a href="http://photo.stackexchange.com/" title="pro fessional, enthusiast and amateur photographers">Photography</a></li> <li><a href="http://scifi.stackexchange.com/" title="sci ence fiction and fantasy enthusiasts">Science Fiction &amp; Fantasy</a></li> <li><a href="http://cooking.stackexchange.com/" title="p rofessional and amateur chefs">Seasoned Advice (cooking)</a></li> <li><a href="http://diy.stackexchange.com/" title="contr actors and serious DIYers">Home Improvement</a></li> <li> <a href="http://stackexchange.com/sites#lifearts" cl ass="more"> more (13) </a> </li> </ol> </td> <td> <ol> <li><a href="http://english.stackexchange.com/" title="l inguists, etymologists, and serious English language enthusiasts">English Langua ge &amp; Usage</a></li> <li><a href="http://skeptics.stackexchange.com/" title=" scientific skepticism">Skeptics</a></li> <li><a href="http://judaism.stackexchange.com/" title="t hose who base their lives on Jewish law and tradition and anyone interested in l earning more">Mi Yodeya (Judaism)</a></li> <li><a href="http://travel.stackexchange.com/" title="ro ad warriors and seasoned travelers">Travel</a></li> <li><a href="http://christianity.stackexchange.com/" tit le="committed Christians, experts in Christianity and those interested in learni ng more">Christianity</a></li> <li><a href="http://gaming.stackexchange.com/" title="pa ssionate videogamers on all platforms">Arqade (gaming)</a></li> <li><a href="http://bicycles.stackexchange.com/" title=" people who build and repair bicycles, people who train cycling, or commute on bi cycles">Bicycles</a></li> <li><a href="http://rpg.stackexchange.com/" title="gamem asters and players of tabletop, paper-and-pencil role-playing games">Role-playin g Games</a></li> <li> <a href="http://stackexchange.com/sites#culturerecre ation" class="more"> more (21) </a> </li> </ol> </td> <td> <ol> <li><a href="http://math.stackexchange.com/" title="peop le studying math at any level and professionals in related fields">Mathematics</ a></li>

<li><a href="http://stats.stackexchange.com/" title="sta tisticians, data analysts, data miners and data visualization experts">Cross Val idated (stats)</a></li> <li><a href="http://cstheory.stackexchange.com/" title=" theoretical computer scientists and researchers in related fields">Theoretical C omputer Science</a></li> <li><a href="http://physics.stackexchange.com/" title="a ctive researchers, academics and students of physics">Physics</a></li> <li><a href="http://mathoverflow.net/" title="mathematic ians">MathOverflow</a></li> <li> <a href="http://stackexchange.com/sites#science" cla ss="more"> more (7) </a> </li> </ol> </td> <td> <ol> <li><a href="http://stackapps.com/" title="apps, scripts , and development with the Stack Exchange API">Stack Apps</a></li> <li><a href="http://meta.stackoverflow.com/" title="meta -discussion of the Stack Exchange family of Q&amp;A websites">Meta Stack Overflo w</a></li> <li><a href="http://area51.stackexchange.com/" title="pr oposing new sites in the Stack Exchange network">Area 51</a></li> <li><a href="http://careers.stackoverflow.com/">Stack Ov erflow Careers</a></li> </ol> </td> </tr> </tbody></table> </div> </div> <div id="copyright"> site design / logo 2013 stack exchange inc; user contributions licensed under <a href="http://creativecommon s.org/licenses/by-sa/3.0/" rel="license">cc-wiki</a> with <a href="http://blog.stackoverflow.com/2009/06/attributionrequired/" rel="license">attribution required</a> </div> <div id="footer-flair"> <a href="http://creativecommons.org/licenses/by-sa/3.0/" class=" cc-wiki-link"></a> </div> <div id="svnrev"> rev 2013.7.1.798 </div> </div> </div> <noscript> &lt;div id="noscript-warning"&gt;Stack Overflow works best with JavaScri pt enabled&lt;img src="http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif" al t="" class="dno"&gt;&lt;/div&gt; </noscript>

<script type="text/javascript">var _gaq=_gaq||[];_gaq.push(['_setAccount','U A-5620270-1']); _gaq.push(['_setCustomVar', 1, 'tags', '|c#|fibonacci|codility|']); _gaq.push(['_trackPageview']); var _qevents = _qevents || []; (function(){ var s=document.getElementsByTagName('script')[0]; var ga=document.createElement('script'); ga.type='text/javascript'; ga.async=true; ga.src='http://www.google-analytics.com/ga.js'; s.parentNode.insertBefore(ga,s); var sc=document.createElement('script'); sc.type='text/javascript'; sc.async=true; sc.src='http://edge.quantserve.com/quant.js'; s.parentNode.insertBefore(sc,s); })(); </script> <script type="text/javascript"> _qevents.push({ qacct: "p-c1rF4kxgLUzNc" }); </script> </body></html>

Das könnte Ihnen auch gefallen