n-th padovan string p(n), JAVA Programming

write a program that count the number of occurences of 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 1:18:56 AM | Location : United States







Related Discussions:- n-th padovan string p(n), Assignment Help, Ask Question on n-th padovan string p(n), Get Answer, Expert's Help, n-th padovan string p(n) Discussions

Write discussion on n-th padovan string p(n)
Your posts are moderated
Related Questions
Illustrate the method to print on the GUI Example Code: Taking Input / Output So far, we learned how to print something on console. Now time has come to learn how to print

Visa Resolve Online and Health Care Project: Visa Resolve Online In this project there are few modules like TRANSACTION INQUIRY,CHARGEBACK , REPRESENTMENT, ARBITRATION .

#question)i) devising ii) validating and iii) testing of algorithms...

We need to create an Android application. The application is about registering courses in a university, viewing grades, available courses and evaluating faculty members. Advisor

Write a CGI program in R, accessible through the class web server, that performs an analysis or creates a graph. You can provide a separate HTML page that displays the initial form

Need to develop a Small Android App using Text to speech and Speech to Text feature. Project Description: When the Android App Opens, It will connect to Windows Azure cloud,

What do you think about an object's lock and which kinds of object's have locks?

Describe Importing Classes in java? Fully qualified names such as java.net.URL are not the most convenient thing to have to type. You can use the shorter class names like URL w

How much CPU time does an applet get? One of the few legitimate concerns about hostile applets is excessive use of CPU time. It is possible on a non-preemptively multitasking s

Explain java.util.Random package? The java.util.Random class permits you to create objects which generates pseudo-random numbers along with uniform or gaussian distributions ac