You are not logged in. Please login at to post your questions!


fibonacci modified

i found this question on hackerrank. can anyone explain how this can be solved using c++ .

asked 26 Oct '18, 05:44

sumesh_pandit's gravatar image

accept rate: 0%

@sumesh --

The basics of the solution is a simple loop. This is pseudocode...

    declare t_Nminus2 := t1  // function parameter
    declare t_Nminus1 := t2  // function parameter
    declare t_N

    for i := 3 to n inclusive  // n is a function parameter
        t_N := t_Nminus2 + t_Nminus1 * t_Nminus1
        t_Nminus2 := t_Nminus1
        t_Nminus1 := t_N
    end for

    return t_N

The challenge isn't the base algorithm outlined above. The challenge is the datatype to use for t_N.

Since n can be up to 20 and the next term includes the square of the prior term, the max possible value of t_N is much larger than ULLONG_MAX (largest possible unsigned long long int), as the HackerRank problem statement says. There is no native data type in C++ that can handle such large integers, so it's possible this problem wants you to create your own datatype.


answered 27 Oct '18, 23:11

sbatten1969's gravatar image

accept rate: 16%

edited 27 Oct '18, 23:12

toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 26 Oct '18, 05:44

question was seen: 108 times

last updated: 27 Oct '18, 23:12