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

Assignment Help:

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
));
    }
}

Related Discussions:- A Padovan string P(n) for a natural number n

Luminous Jewels - The Polishing Game, Byteland county is very famous for lu...

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec

Type compatibility, Define  T y pe c o m p a t i b i li t y? ...

Define  T y pe c o m p a t i b i li t y? T o a s s i g n i n t t o s m a l l i n t t h e v a r i a b l e should b e

Arrays, how to declare multi dimensional array

how to declare multi dimensional array

Example for register storage class - computer programming, Example for Regi...

Example for Register Storage Class - computer programming? main() { register int i; for (i=0; i { ............... ............... } } /* block exit will free the register

Pointers, Pointers are just numbers, representing addresses in memory. You ...

Pointers are just numbers, representing addresses in memory. You can add to and subtract from pointers, for instance. It is pretty easy to make mistakes with pointer math, however,

String program, Write a C++ program which does the following: 1. Asks th...

Write a C++ program which does the following: 1. Asks the user to enter the following text "Four score and seven years ago there was a man named Joey Bagadonuts." 2. Save thi

Area, write a program to find the area under the curve y=f(x) between x=a &...

write a program to find the area under the curve y=f(x) between x=a & x=b

How to write an inline class member function, How to write an inline class ...

How to write an inline class member function In addition to global functions, you may request that non-static member functions of a class be inlined.  The normal method of doin

kbbpuc

1/28/2013 7:30:27 AM

V4TRRu kwmeezykbbns,">http://kwmeezykbbns.com/">kwmeezykbbns, [url=http://hoqcfhngzhzz.com/]hoqcfhngzhzz[/url], [link=http://olnulsgfmzta.com/]olnulsgfmzta[/link], http://vaqeasygbayc.com/

Zdykinir

1/31/2013 12:32:29 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,

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd