Generalizing Fibonacci sequence with SICStus Prolog

后端 未结 6 2011
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-19 08:00

I\'m trying to find a solution for a query on a generalized Fibonacci sequence (GFS). The query is: are there any GFS that have 885 as their 12th number? The initial 2 numbe

相关标签:
6条回答
  • 2021-01-19 08:33

    Without the base cases, fib/2 has no solution; no matter how you call it in fib2. Note: if you use recursion, you need at least one base case.

    0 讨论(0)
  • 2021-01-19 08:46

    Define a predicate gfs(X0, X1, N, F) where X0 and X1 are the values for the base cases 0 and 1.

    0 讨论(0)
  • 2021-01-19 08:52

    Under SWI-Prolog:

    :- use_module(library(clpfd)).
    
    fib(A,B,N,X):-
        N #> 0,
        N0 #= N-1,
        C #= A+B,
        fib(B,C,N0,X).
    fib(A,B,0,A).
    
    task(A,B):-
        A in 1..10,
        B in 1..10,
        fib(A,B,11,885).
    
    0 讨论(0)
  • 2021-01-19 08:55

    I'd say you're doing something terribly wrong... When you call fib(1, X1), the variable X1 is the number that the function fib will return, in this case, it will be 1, because of the base case fib(1, 1)..

    0 讨论(0)
  • 2021-01-19 08:55

    Consider fib(N,F1,F2) so you'll be able to replace fib(Nmin1, Xmin1) and fib(Nmin2, Xmin2) with simple fib(Nmin2, Xmin2, Xmin1).

    0 讨论(0)
  • 2021-01-19 08:57

    Maybe not a solution in the strict sense but I will share it never the less. Probably the only gain is to show, that this does neither need a computer nor a calculator to be solved. If you know the trick it can be done on a bearmat.

    If F_n ist the n-th Term of the ordinary Fibo-sequence, starting with F_1=F_2=1, then the n-th Term of the generalized sequence will be G_n = F_{n-2}*a+F_{n-1}*b. Define F_{-1}=1, F_0 = 0

    (Indeed, by induction

    • G_1 = F_{-1}*a+F_0*b = 1*a+0*b=a
    • G_2 = F_0 * a + F_1 * b = 0*a + 1*b = b
    • G_{n+1} = F_{n-1}a + F_nb = (F_{n-3} + F_{n-2} )a + (F_{n-2} + F_{n-1})*b = G_n + G_{n-1}

    )

    Thus G_12 = F_10 * a + F_11 * b = 55a + 89b.

    Now you can either search for solutions to the equation 55a + 89b = 885 with your computer

    OR

    do the math:

    Residues mod 11 (explanation):

    55a + 89b = 0 + 88b + b = b; 885 = 880 + 5 = 80*11 + 5 = 5

    So b = 5 mod 11, but since 1 <= b <= 10, b really is 5. 89 * 5 = 445 and 885-445 = 440. Now, divide by 55 and get a=8.

    0 讨论(0)
提交回复
热议问题