In: Computer Science
Prolog,
Please create a prolog predicate pell(N,Pn), which is true if 'Pn' is Nth term of the sequence of Pell Numbers. such as 'pell(7,169)' results in true and pell(6,Pn)' results in 70.
Pell sequence = 0,1,2,5,12,29,70,169,408,..
Given the pell sequence = 0,1,2,5,12,29,70,169,408..
First let us know the logic of the sequence.The sequence goes as follows-each number is doubled then added to the number that precedes it.
1.1+1=2
2.2+2+1=5 i.e.,2 is doubled(by adding it to itself) then added to 1 which is the number directly preceding it
3.5+5+2=12i.e.,5 is doubled(by adding it to itself) then added to 2 which is the number directly preceding it.
The rest of the sequence is below:(The same rule applies)
4.12+12+5=29
5.29+29+12=70
6.70+70+21=169
7.169+169+70=408
Java code for the sequence:
import java.util.Scanner;
public class main
{
public static void main(String args[])
{
int n,a=1,b=0,c;
for(n=1;n<=10;n++)
{
c=a+2*b;
System.out.print(c+" ");
a=b;
b=c;
}
}
}
From the program we can know that the logic is
Pn=2*Pn-1+Pn-2
with P0=0and P1=1
The result will be stored in an array and extract the numbers based on thier index which is starting from 0 and ends at n-1 where n is the total elements in an array.
In the array give the index for the elements and get the elements by its key value and store in the form of pell(N,PN) where N is the index of the element and PN is the value of the element at Nth index.
In pell(N,PN),if Nth index contains the element PN then return true else return false.
In other case,index is given and need to find the element at that index.By using array indexing we find the element in that particular indexing and print the value of the element.