Guest User

Untitled

a guest
Jun 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. /*
  2. The Fibonacci sequence is defined by the recurrence relation:
  3. Fn = Fn-1 + Fn-2, where F1 = 1 and F2 = 1.
  4. Hence the first 12 terms will be:
  5.  
  6. F1 = 1
  7. F2 = 1
  8. F3 = 2
  9. F4 = 3
  10. F5 = 5
  11. F6 = 8
  12. F7 = 13
  13. F8 = 21
  14. F9 = 34
  15. F10 = 55
  16. F11 = 89
  17. F12 = 144
  18. The 12th term, F12, is the first term to contain three digits.
  19. What is the index of the first term in the Fibonacci sequence to contain 1000 digits?
  20.  
  21. Result: 4782
  22. */
  23.  
  24. class Pair<T, R> {
  25. final T first;
  26. final R second;
  27.  
  28. Pair(this.first, this.second);
  29.  
  30. @override
  31. String toString() => 'Pair{ $first, $second }';
  32. }
  33.  
  34. Iterable<Pair<int, int>> get fibonacciSequenceWithIndex sync* {
  35. var f = 1;
  36. var f1 = 1;
  37. var f2 = 1;
  38. var index = 2;
  39.  
  40. yield new Pair(1, 1);
  41. while (true) {
  42. yield new Pair(f, index++);
  43. f = f1 + f2;
  44. f1 = f2;
  45. f2 = f;
  46. }
  47. }
  48.  
  49. int solve() => fibonacciSequenceWithIndex
  50. .firstWhere((pair) => pair.first.toString().length >= 1000)
  51. .second;
  52.  
  53. main(List<String> arguments) {
  54. print(solve());
  55. }
Add Comment
Please, Sign In to add comment