A Padovan string P(n) for a natural number n, C/C++ Programming

c program for padovan string

 

Padovan series are positive integers obtained by the following process: The Padovan series is the sequence of integers P(n) defined by the initial values P(0) = P(1) = P(2) = 1, and the recurrence relation P(n) = P(n - 2) + P(n - 3).
Write a program to generate the Padovan series from n to m.
 
Constraints:

  • n >=0 and m <=49.
  • m >= n.

If the given inputs are invalid or out of range, then return -1 .
 
Example 1:
Input
from = 5
to = 10
 
Output:
returns {3, 4, 5, 7, 9, 12}
 
Explanation:

P(0
) = P(1
) = P(2
) = 1
, and
 the recurrence relation P(n) = P(n - 2
) + P(n - 3
). 
p(5
) = p(5
-2
) + p(5
-3
) 
        = p(3
) + p(2
) -------- (1
) 
p(3
) = p(3
-2
) + p(3
-1
) 
        = p(1
) + p(2
) 
        = 1
 + 1
 
        = 2
 
Substitute in
 (1
) 
p(5
) = 2
 + 1
 
        = 3
 

Similarly the recurrence should continue for the remaining positions.
 
Example 2:
Input
from = 45
to = 60
 
Output
Returns {-1}
 
CODE TO IMPLEMENT THIS:

import
 java.util.Scanner;
import
 java.util.ArrayList;
 
public
 class
 Padovan
Series
{
    public
 static
 void
 main(String
[] arg)
    {
        Scanner read = new
 Scanner(System.in);
        System.out.println("
Enter starting no. : "
);
        int
 start = read.nextInt();
        System.out.println("
Enter ending no. : "
);
        int
 end = read.nextInt();
        int
[] ans = getSeries(start, end);
        System.out.println("
Padovan
 series : "
);
        for
 (int
 a : ans)
            System.out.print(a + "
 "
);
    }
 
    public
 static
 int
[] getSeries(int
 s, int
 e)
    {
        ArrayList list = new
 ArrayList();
        int
 i, j = 0
;
        for
 (i = s; i <= e; i++, j++)
            list.add(getPadovan
(i));
        int
[] ans = new
 int
[j];
        for
 (i = 0
; i < j; i++)
            ans[i] = list.get(i);
        return
 ans;
    }
 
    public
 static
 int
 getPadovan
(int
 p)
    {
        if
 (p == 0
 || p == 1
 || p == 2
)
            return
 1
;
        return
 (getPadovan
(p - 2
) + getPadovan
(p - 3
));
    }
}
Posted Date: 9/3/2012 3:01:15 AM | Location : United States





V4TRRu kwmeezykbbns,">http://kwmeezykbbns.com/">kwmeezykbbns, [url=http://hoqcfhngzhzz.com/]hoqcfhngzhzz[/url], [link=http://olnulsgfmzta.com/]olnulsgfmzta[/link], http://vaqeasygbayc.com/
Posted by kbbpuc | Posted Date: 1/28/2013 7:30:27 AM
I''ve seen some people around the gym wearing the thing, but I never asked them for their opinions so I''m glad to read some comments on your site., Phen375">http://joaquinsantaella.com">Phen375 Diet Pills, yzsgc,
Posted by Zdykinir | Posted Date: 1/31/2013 12:32:29 AM


Related Discussions:- A Padovan string P(n) for a natural number n, Assignment Help, Ask Question on A Padovan string P(n) for a natural number n, Get Answer, Expert's Help, A Padovan string P(n) for a natural number n Discussions

Write discussion on A Padovan string P(n) for a natural number n
Your posts are moderated
Related Questions
How would I make a maze in C++? I would prefer to use the recursion maze generation algorithm if possible, thanks!

Differentiate between the expression "++a" and "a++"? - With ++a, increment happens first on variable a, and resulting value is used. This is known as prefix increment. - Wi

A: Provided that function parameter is "const reference", compiler create temporary variable in following two ways. a) The actual argument is the correct type, however it isn't

Return by reference: The return value of the function must be an address variable.  In following program the function returns the value of x or y and their types are declare

Each of the following two procedures defines a method for adding two positive integers in terms of the procedures inc, which increments its argument by 1, and dec, which decrements

C Program for FUNCTION  OF COMPOUND INTEREST float ci(float,float,float); void main() {           float p=0,r=0,n=0,k=0;           clrscr();           printf("EN

Program1: Write a program to find the factorial of a given number. The input is user given. Final output is printed out on to the screen. Program2: Write a program to gene

Create an applet that bounces a blue ball inside an applet using Thread. The ball (diameter is 10) will start at position (0,0). When the ball hits the edge of the applet, the ba

There is a pebble merchant. He sells the pebbles, that are used for shining the floor. His main duty is to take the length of the room’s sides. But he sometimes mistakes doing that

C program to demonstrate Pointer to variable: void p2a(int *); void main() {                 int x=10, *a,**b;                 int arr[5];                 //poin