Padovan string, C/C++ Programming

write a program that counts the number of occurrences of the string in the n-th Padovan string P(n)

 

program in java

// aakash , suraj , prem sasi kumar kamaraj college
program 1 :

package test.padovanstring;

public class PadovanString {
    public int stringOccurrences(int n, String str){
   
    if(n >= 40)
    return -1;
    if(str.replaceAll("X|Y|Z","").length()>0)
    return -1;
    String res=pad(n);
    return(res.length() - res.replaceAll(str,"").length())/(str.length());
    }
public String pad(int n){
    if(n == 0) return "X";
    if(n == 1) return "Y";
    if(n == 2) return "Z";
    else return pad(n-2) + pad(n-3);
   
}
    public static void main(String []args)
    {
        PadovanString p = new PadovanString();
        System.out.println(p.stringOccurrences(21,"YZ"));
    }
}

 

 

 

Program set 2 :

import java.util.Scanner;

import java.util.ArrayList;

 

public class PadovanSeries

{

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

    }

}

 

Padova

Posted Date: 9/3/2012 3:44:42 AM | Location : United States







Related Discussions:- Padovan string, Assignment Help, Ask Question on Padovan string, Get Answer, Expert's Help, Padovan string Discussions

Write discussion on Padovan string
Your posts are moderated
Related Questions
Differences between a pointer and a reference 1.  A reference must always point to some object where as this restriction is not imposed on a pointer. e.g. int *pi = 0;

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

I started preparing an alternative cryptocurrency and I am way over my head. I would like help in making modifications to the open source files and then help with deployment. I wil

A company needs 200 pencils per year . you cannot simply use this price as the cost of pencils two years from now. Because of inflation the cost is likely to be higher than it is

Question: a) Give two uses of NFS. b) Give one advantage of using NFS. c) Explain the following lines as they would be interpreted in the /etc/exports file i. /usr/local

I need vsti program Project Description: Knowledge of vsti programs, and music is a plus, ex: nexus I will input the sounds I just need the interface built Skills required

Write algorithms to implement the insertion and deletion operations.

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

write a C++ program that performs the following tasks 1. prompts the user to enter two numbers 2. displays the divisors of each number and store the divisors of number 1 in A and t

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b