Code :

import java.util.Scanner;
public class Recursion {
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
long n, result1, result2, startTime1, stopTime1, startTime2, stopTime2;
System.out.println("Please Enter A Number in between 0 and maxValue, will be calcualtred using 2 methods....");
n = kb.nextLong();
startTime1 = System.nanoTime();
result1 = fibRecursive(n);
stopTime1 = System.nanoTime();
startTime2 = System.nanoTime();
result2 = fibLoop(n);
stopTime2 = System.nanoTime();
System.out.println("\nDisplaying solution for recursive method "+ result1 + "Time Taken: " + (stopTime1 - startTime1));
System.out.println("\nDisplaying solution for loop method "+ result2 + "Time Taken: " + (stopTime2 - startTime2));
System.out.println("\nThanks for using our fibnoacci calculator. ");
}
public static long fibRecursive(long i)
{
if(i == 0)
return 0;
else if(i == 1)
return 1;
else
return fibRecursive(i - 1) + fibRecursive(i - 2);
}
public static long fibLoop(long k)
{
long a = 0, b = 1, ans = 0;
for(int i = 1; i < k; i++)
{
ans = a + b;
a = b;
b = ans;
}
return ans;
}
}

is there perhaps a built in function/feature in java to determine this number? In my case I am looking for the max value for "long n". Any help is great appreciated, thanks. ]]>

I have an index that is encoded as such:

Code :

int idx = x << 11 | z << 7 | y;

my question is: can i go from idx back to x, z, y?

If so, how?

If not.. crap. ]]>

Problem: Given a string S and a string T, count the number of distinct subsequences of T in S.

A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE" is a subsequence of "ABCDE" while "AEC" is not).

Here is an example:

S = "rabbbit", T = "rabbit"

Return 3.

Solution:

public int numDistincts(String S, String T) {

int[][] table = new int[S.length() + 1][T.length() + 1];

for (int i = 0; i < S.length(); i++)

table[i][0] = 1;

for (int i = 1; i <= S.length(); i++) {

for (int j = 1; j <= T.length(); j++) {

if (S.charAt(i - 1) == T.charAt(j - 1)) {

table[i][j] += table[i - 1][j] + table[i - 1][j - 1];

} else {

table[i][j] += table[i - 1][j];

}

}

}

return table[S.length()][T.length()];

} ]]>

the formula given to me is 1^k+2^k+...+n^k.

my coding as follows:

Code java:

public int sumPowTo (int n, int k)
{
if ( k == 0)
{
return 1;
}
else if ( k == 1)
{
return n;
}
else
{
return (n * sumPowTo(n,--k)) + (sumPowTo(n--,--k)) ;
}
}

for example int sumPowTo(4,2) = 30

but i get 28 ]]>

I am currently studying this algorithm...

Can anyone please give me a simple code in doing this algorithm in Java?

Thanks in advance :) ]]>