Tuesday, September 8, 2009

Euler Project 104

My Solution Using Mathematica,but this method is so bad which waste more than one hour to get the result.
The Fibonacci sequence is defined by the recurrence relation:
Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1.
It turns out that F541, which contains 113 digits, is the first Fibonacci number for which the last nine digits are 1-9 pandigital (contain all the digits 1 to 9, but not necessarily in order). And F2749, which contains 575 digits, is the first Fibonacci number for which the first nine digits are 1-9 pandigital.
Given that Fk is the first Fibonacci number for which the first nine digits AND the last nine digits are 1-9 pandigital, find k.



{a, b} = {Fibonacci[200000], Fibonacci[200001]}

For[i = 200000, i < 10000000, i++, Tempvalue = a;
 If[Sort[IntegerDigits[Tempvalue][[-9 ;; -1]]] == PandigitalList && 
   Sort[IntegerDigits[Tempvalue][[1 ;; 9]]] == PandigitalList, 
  Print[i], {a, b} = {b, a + b}]]

Others' Solution
First Solution
NextFib[{n_, f1_, f2_}] := {n + 1, f2, f1 + f2}~Mod~1000000000

Fib[n_] := GoldenRatio^n/Sqrt[5] // N // Round

NotPanQ[X_] := Sort[X] ≠ {1, 2, 3, 4, 5, 6, 7, 8, 9}

Test[{n_, f1_, f2_}] :=
NotPanQ @ IntegerDigits[f2] ||
NotPanQ @ Select[IntegerDigits@Fib[n], True &, 9]
NestWhile[NextFib, {2, 1, 1}, Test]

Second Solution
(defparameter *a* 1) 
(defparameter *b* 1) 
(defparameter *i* 1)
  
(defun problema () 
    (let ((a 1) (b 1) c) 
        (do ((i 1 (1+ i))) 
             ((and (pandigital-p (format nil "~a" a)) 
                    (pandigital-p (substring (format nil "~a" (fibo i)) 0 9))) 
                 i) 
            (setf c (mod (+ a b)1000000000) a b b c))))
  
(defun fibo (n) 
    (loop 
        for i from *i* to (1- n) 
        with
        do 
        (setf c (+ *a* *b*) *a* *b* *b* c *i* (1+ i)) 
        finally (return *a*)))
  
(defun pandigital-p (str)
(and (find #\1 str)(find #\6 str)
(find #\2 str)(find #\7 str)(find #\3 str)
(find #\8 str)(find #\4 str)(find #\9 str)
(find #\5 str)))
  
(time (print (problema)))

Third Solution
(defun number->digits-h (x digits) 
    (if (= x 0) 
         digits 
        (number->digits-h (/ (- x (mod x 10)) 10) (cons (mod x 10) digits))))
  
(defun number->digits (x) 
    (number->digits-h x ()))
  
(defun check-first-9 (number) 
    (let* ((digits (1+ (floor (log number 10)))) 
            (nl (/ (- number (mod number (expt 10 (- digits 9)))) 
                    (expt 10 (- digits 9)))) 
            (first-9 (number->digits nl))) 
        (= 9 (length (remove 0 (remove-duplicates first-9))))))
  
(defun euler104 () 
    (do* ((a 1 b) 
        (b 1 c) 
        (c (+ a b) (+ a b)) 
        (n 3 (1+ n)) 
        (last 2 (mod c (expt 10 9)))) 
        ((and (= 9 (length (remove 0 (remove-duplicates (number->digits last))))) 
              (check-first-9 c)) (print n)) 
        (if (zerop (mod n 10000)) (print n))))

Forth Solution
Module[{k = 3, f = {0, 1, 1}, fk},
While[True,
fk = Mod
[f[[Mod[k - 1, 3] + 1]] + f[[Mod[k - 2, 3] + 1]], 10^9];
f
[[Mod[k, 3] + 1]] = fk;
If[Sort@IntegerDigits@fk == Range[9],
If[StringJoin@
Sort@Characters@StringTake
[ToString@Fibonacci@k, 9] ==
"123456789", Break[]]]; k++]; k]

No comments:

Post a Comment