ง่าย 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

First Fibonacci With N Digits

Find the position of the first Fibonacci number that has n digits.

สิ่งที่คุณจะได้เรียนรู้

  • Add two digit arrays
  • Generate Fibonacci terms without overflow
  • Stop at the first term that meets a condition

โจทย์

ดูฉบับแปล

The Fibonacci sequence starts F(1) = 1, F(2) = 1, and each next term is the sum of the two before it: 1, 1, 2, 3, 5, 8, 13, 21, ... Find where the sequence first reaches n decimal digits. The terms soon outgrow 64-bit integers, so add them as digit arrays if your language has no big integers.

Implement fibonacci_with_n_digits(n).

Input

n: The number of digits.

Output

Return the index i of the first term F(i) with at least n digits. For n = 1 the answer is 1.

เงื่อนไข

1 ≤ n ≤ 1000

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
n = 2

ผลลัพธ์ที่คาดหวัง:
expected = 7
อินพุต:
long long n = 2;

ผลลัพธ์ที่คาดหวัง:
long long expected = 7;
อินพุต:
long n = 2;

ผลลัพธ์ที่คาดหวัง:
long expected = 7;

คำอธิบาย

F(6) = 8 has one digit and F(7) = 13 has two, so the answer is 7.

ตัวอย่างที่ 2
อินพุต:
n = 4

ผลลัพธ์ที่คาดหวัง:
expected = 17
อินพุต:
long long n = 4;

ผลลัพธ์ที่คาดหวัง:
long long expected = 17;
อินพุต:
long n = 4;

ผลลัพธ์ที่คาดหวัง:
long expected = 17;

คำอธิบาย

F(16) = 987 and F(17) = 1597, so the answer is 17.

แนวทางการแก้

ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ

คำใบ้ 1

Keep only the last two terms.

คำใบ้ 2

Store each term as digits and add them with carries.

คำใบ้ 3

Count the index as you go, starting from F(1) = 1.

ความซับซ้อนที่คาดหวัง

O(n²) time, O(n) extra space

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

ยังไม่มีคำตอบที่ผ่าน แก้โจทย์นี้แล้วคว้าอันดับหนึ่งไปเลย

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก