I used method a that counts the amount of possible choices for getting like number 10 with numbers that are from 0 to 6. Problem is that it just takes too much time when x is like 50 or something. I just need some tips what I should do to make this faster.
Code
public static int count(int x) {
if (x < 0) {
return 0;
}
if (x == 0) {
return 1;
}
int result = 0;
for (int i = 1; i <= 6; i++) {
result += count(x - i);
}
return result;
}
This is a variation on Fibonacci except it is the sum of the last six values instead.
You can use a plain loop which will be faster than memorisation (the first time)
public static long count(int x) {
long a=0, b=0, c=0, d=0, e=0, f=1;
while(x-- > 0) {
long sum = a + b + c + d + e + f;
a = b; b = c; c = d; d = e; e = f;
f = sum;
}
return f;
}
If you call this repeatedly you may as well store all the values in the int range which is likely to be less than 30 the first time and retrieve these values after that.
Related
public class StairCase {
public static int Solution(int n) {
int sum, count = 0;
//Funtion to calculate stair case and use factorial because a solution can have many ways.
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
for (int k = 0; k <= n; k++) {
sum = i + (j * 2) + (k * 3);
if (sum == n) {
count = count + (fact.fact1(i + j + k) / (fact.fact1(i) * fact.fact1(j) * fact.fact1(k)));
}
}
}
/*logic behind this funtion is that like we have to find no of arrangement abbbccde can we written so we write (8!/(3!*2!))
so after getting like ijk= 1,2,3 I rearranged them in that man ways
(i+j+k)!/i!*j!k! !=factorial/
}
return count;
}
public static void doTestPass() {
boolean result = true;
result = result && (Solution(3) == 4);
result = result && (Solution(4) == 7);
result = result && (Solution(11) == 504);
result = result && (Solution(12) == 927);
result = result && (Solution(13) == 1705);
//working fine till 13 not after that
System.out.println(Solution(14));
System.out.println(Solution(20));
System.out.println(Solution(21));
//14--3127 20--68603 21--94351(orignal ans-- 3136,121415,223317)
if (result) {
System.out.println("All Test Cases Passed");
} else {
System.out.println("Test Case Failed");
}
}
public static void main(String[] Args) {
doTestPass();
}
public class fact
{
static int fact1(int n)
{
//this funtion is to create factorial
int res = 1;
for (int i = 2; i <= n; i++)
{
res = res * i;
}
return res;
}
}
}
The problem has to do with using primitive integers for your factorial method and overflowing the integer limit of 2,147,483,647.
Take a look at trial runs of your factorial code below:
fact1(12) outputs 479001600 which is correct for 12!
fact1(13) outputs 1932053504 which is not 13! which should be 6227020800
This means when you execute fact.fact1(i) in your code, it will be outputting the wrong answer for any value greater than 12. This would require you to rework the data structure you use to hold these big numbers to BigInteger.
The reworked fact method would look like this:
BigInteger fact1(int n) {
// this funtion is to create factorial
BigInteger res = BigInteger.ONE;
for (int i = 2; i <= n; i++) {
res = res.multiply(BigInteger.valueOf(i));
}
return res;
}
With the output of fact1(13) being the correct value of 6227020800 now.
You will need to rework the other aspects of your code now with BigInteger, but you should understand now what your issue was.
I was solving the Magical Number Problem where the number at nth position is the sum of the previous 3 numbers, minus 1. For example: 0 1 1 1 2 3 5 9 16.... and so on.
I solved it in 2 ways.
Code 1) Using Recursion
int magicNumber(int n){
int f = 0;
if (n == 1)
return 0;
else if (n > 1 && n <= 4)
return 1;
else
f = (magicNumber(n-1) + magicNumber(n-2) + magicNumber(n-3)) - 1;
return f;
}
Code 2) Using Array
void magicNumber(int n){
long arr[] = new long[100];
int i=1;
for(i = 1; i <= n; i++)
{
if(i==1)
arr[i] = 0;
else if(i>1&&i<=4)
arr[i] = 1;
else
arr[i] = (arr[i-1] + arr[i-2] + arr[i-3]) - 1;
}
System.out.println("Result is : "+arr[n]);
}
Code 1 works fine when I provide a small integer number to the program, but it hangs with the input of bigger integer numbers and Code 2 runs fine without any problem.
So I need your suggestions, how can I improve the performance of the recursion program Code 1?
You can speed up your recursion like this:
int magicNumber2(int n, int a, int b, int c){
if (n <= 1) return a;
return magicNumber2(n - 1, b, c, a + b + c - 1);
}
int magicNumber(int n) {
magicNumber2(n, 0, 1, 1);
}
You're experiencing delay for higher numbers because each recursive call ends up in 3 more recursive calls. Hence the time rises exponentially. Try this approach:
Maintain a lookup table. Here I have an array magic_num[100] with all it's elements initialized to -1.
int magicNumber(int n){
if(n == 1)
{
magic_num[n] = 0;
return 0;
}
else if(n>1 && n<=4)
{
magic_num[n] = 1;
return 1;
}
else if(magic_num[n] == -1)
{
magic_num[n] = magicNumber(n-1) + magicNumber(n-2) + magicNumber(n-3) - 1;
return magic_num[n];
}
else
return magic_num[n];
}
The question here would be to get the sum of powers (m^0 + m^1 + m^2 + m^3.... + m^n) using only FOR loops. Meaning, not using any other loops as well as Math.pow();
Is it even possible? So far, I am only able to work around getting m^n, but not the rest.
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
int total = 1;
System.out.print("Enter value of m: ");
int m = scn.nextInt();
System.out.print("Enter value of n: ");
int n = scn.nextInt();
for (int i = 1; i <= n; i++){
total * m;
}
System.out.print(total);
}
Let's say m =8; and n = 4;
i gives me '1,2,3,4' which is what I need, but I am unable to power m ^ i.
Would be nice if someone could guide me into how it could be done, can't seem to progress onwards as I have limited knowledge in Java.
Thanks in advance!
You might want to rewrite it like this :
m^0 + m^1 + m^2 + m^3.... + m^n = 1 + m * (1 + m * (1 + m * (.... ) ) )
And you do it in a single for loop.
This should do the job (see explanations in comments):
public long count(long m, int pow) {
long result = 1;
for(int i = 0;i<pow; i++) {
result*=m +1;
}
return result;
}
You can nest loops. Use one to compute the powers and another to sum them.
You can do below:
int mul = 1;
total = 1;
for(int i=1;i<=n;i++) {
mul *= m;
total += mul;
}
System.out.println(total);
You can use a single loop which is O(N) instead of nested loops which is O(N^2)
long total = 1, power = m
for (int i = 1; i <= n; i++){
total += power;
power *= m;
}
System.out.print(total);
You can also use the formula for geometric series:
Sum[i = k..k+n](a^i) = (a^k - a^(k+n+1)) / (1 - a)
= a^k * (1 - a^(n+1)) / (1 - a)
With this, the implementation can be done in a single for loop (or 2 simple for loop): either with O(n) simple looping, or with O(log n) exponentiation by squaring.
However, the drawback is that the data type must be able to hold at least (1 - a^(n+1)), while summing up normally only requires the result to fit in the data type.
This is the solution :
for(int i=0;i<n;i++){
temp=1;
for(int j=0;j<=i;j++){
temp *= m;
}
total += temp;
}
System.out.println(total+1);
You can easily calculate powers using your own pow function, something like:
private static long power(int a, int b) {
if (b < 0) {
throw new UnsupportedOperationException("Negative powers not supported.");
}
if (b == 0) {
return 1;
}
if (b == 1) {
return a;
}
return a * power(a, b - 1);
}
Then simply loop over all the values and add them up:
long out = 0;
for (int i = 0; i <= n; ++i) {
out += power(m, i);
}
System.out.println(out);
I would add that this is a classic dynamic programming problem as m^n is m * m^(n-1). I would therefore add caching of previously calculated powers so that you don't have to recalculate.
private static Map<Integer, Long> powers;
public static void main(String args[]) {
int m = 4;
int n = 4;
powers = new HashMap<>();
long out = 0;
for (int i = 0; i <= n; ++i) {
out += power(m, i);
}
System.out.println(out);
System.out.println(powers);
}
private static long power(int a, int b) {
if (b < 0) {
throw new UnsupportedOperationException("Negative powers not supported.");
}
if (b == 0) {
return 1;
}
if (b == 1) {
return a;
}
Long power = powers.get(b);
if (power == null) {
power = a * power(a, b - 1);
powers.put(b, power);
}
return power;
}
This caches calculated values so that you only calculate the next multiple each time.
Hey I took part in Facebook Hacker Competition and I got a solution of this question.
I solved th question in order O(K^2) but this guy solved it in O(^K).
Please explain me what the code doing.
import java.io.File;
import java.util.Scanner;
public class FindTheMin {
private long getNextMin(long[] map, long start, long k) {
while (map[(int)(start)] > 0)
start++;
return start;
}
public long findNth(long n, long k, long a, long b, long c, long r) {
long[] cache = new long[100010];
long[] map = new long[100010];
long pre = a;
for (int i = 0; i < k; i++) {
long num;
if (i == 0)
num = a;
else
num = (b * pre + c) % r;
cache[i] = num;
if (num <= k + 1)
map[(int) num]++;
pre = num;
}
pre = getNextMin(map, 0, k);
cache[(int)k] = pre;
map[(int)pre]++;
for (int i = 0; i <= (int)k; i++) {
long deque = cache[i];
if (deque > k) {
long x = getNextMin(map, pre, k);
cache[i] = x;
map[(int)x]++;
pre = x;
} else {
if (deque < pre) {
if(map[(int)deque] == 1) {
cache[i] = deque;
pre = deque;
continue;
}
}
map[(int)deque]--;
long x = getNextMin(map, pre, k);
cache[i] = x;
pre = x;
map[(int)x]++;
}
}
return cache[(int)((n - 1) % (k + 1))];
}
public static void main(String[] args) {
try {
Scanner s = new Scanner(new File("in.txt"));
int m = s.nextInt();
FindTheMin f = new FindTheMin();
for (int i = 1; i <= m; i++) {
int n, k, a, b, c, r;
n = s.nextInt();
k = s.nextInt();
a = s.nextInt();
b = s.nextInt();
c = s.nextInt();
r = s.nextInt();
System.out.println("Case #" + i + ": " + f.findNth(n, k, a, b, c, r));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
And what are the best ways to learn things like this. the best way to learn java?
This was the question
https://www.facebook.com/hackercup/problems.php?pid=494433657264959&round=185564241586420
map[(int)x]++ does the same as the following:
int index = (int) x;
map[index]++;
What is map[(int)x]++;What it does?
arrays in java accepts only an integer index, but x in your code is a long value.
therefore it has to be casted to int:
This code gets element at index x from array map and adds 1 to the value:
same as:
map[ix]++; // where ix = (int) x;
Question 2:
And what are the best ways to learn things like this. the best way to learn java?
Buy, read and understand:
Robert Sedgewick, Algorithms Fourth Edition
In
long x = getNextMin(map, pre, k);
cache[i] = x;
map[(int)x]++;
While x is a long, the indexes of an array may only be int. Furthermore, and more importantly, although getNextMin is returning a long, the programmer knows the value is actually an int (i.e. x <= Integer.MAX_VALUE) otherwise there could be possible overflow resulting in a negative value for x which would cause an ArrayIndexOutOfBoundsException exception.
Now because java is statically typed, although the value returned by getNextMin fits into an int, you still must tell the compiler your intention to use it as an int; otherwise the compiler will assume it's a long. That is why you see (int)x -- since array indices must be of type int
For the code itself
map[(int)x]++;
could be written as
map[(int)x] = map[(int)x] + 1;
or as
int y = (int) x
map[y]++; // or as map[y] = map[y] + 1
Again, if the value returned by getNextMin is greater than Integer.MAX_VALUE the code will throw an exception. And so it may be a design flaw that the programmer is not handling the potential exception.
I am learning java myself these days. This function is to calculate the Combination. However, I found that there is a very small limit of the number n and k in this function. Each time if I type a large n or k, for instance, 100, it gives me
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Combination.combination(Combination.java:29)
at Combination.main(Combination.java:47)
Or gives me a negative number...
Is there a way to make it work for large number like 10000?
Thanks!
import java.util.HashMap; import java.util.Map;
public class Combination {
private Map<Long,Long> factorialMap = new HashMap<Long,Long>();
public Long getFactorial(int number) {
Long val = factorialMap.get(number);
if(val != null) {
return val;
} else {
val = getFactorialRecursive(number);
factorialMap.put((long) number, val);
return val;
}
}
public Long getFactorialRecursive(int number) {
if(number == 1 || number == 0) {
return 1L;
} else {
return number * getFactorialRecursive(number-1);
}
}
public Long combination(int fromVal, int chooseVal) {
return getFactorial(fromVal)/(getFactorial(chooseVal)*getFactorial(fromVal-chooseVal));
}
public static void main(String[] args) {
int n, k;
Combination comb = new Combination();
java.util.Scanner console = new java.util.Scanner(System.in);
while (true) // will break with k > n or illegal k or n
{ System.out.print ("Value for n: ");
n = console.nextInt();
if ( n < 0 ) break;
System.out.print ("Value for k: ");
k = console.nextInt();;
if ( k > n || k < 0 )
break;
System.out.print(n +" choose " + k + " = ");
System.out.println(comb.combination(n,k));
}
console.nextLine(); // Set up for "Press ENTER...
} }
You should use the BigInteger object: http://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html
In particular, your problem is that 21! is too large for even a long and therefore overflows. Another option would be to use a double, but that will lose precision, so if you need integer accuracy BigInteger is the way to go.
Using BigInteger you will need to convert your integer to BigInteger:
BigInteger bi = new BigInteger(intVal+"");
Then use the add, multiply, divide and subtract (amongst others) to manipulate your values (like):
bi = bi.add(bi2);
Then you can use the method longValue() to get the value back (assuming it fits in a long):
return bi.longValue();
I suggest you consider that Java will not recurse more than about 10,000 times by default and you don't need to calculate such large factorials in the first place.
e.g. 1000!/999! is 1000
If you use a loop you can stop much earlier.
public static BigInteger combination(int n, int r) {
if (r * 2 > n) r = n - r;
BigInteger num = BigInteger.ONE;
BigInteger nr = BigInteger.valueOf(n - r);
for (int i = n; i > r; i--) {
num = num.multiply(BigInteger.valueOf(i));
while (nr.compareTo(BigInteger.ONE) > 0 && num.mod(nr).equals(BigInteger.ZERO)) {
num = num.divide(nr);
nr = nr.subtract(BigInteger.ONE);
}
}
while (nr.compareTo(BigInteger.ONE) > 0) {
num = num.divide(nr);
nr = nr.subtract(BigInteger.ONE);
}
return num;
}
BTW I wouldn't use Long when I mean to use long as it less efficient.
For comparison I have included the same code using long.
public static long combination2(int n, int r) {
if (r * 2 > n) r = n - r;
long num = 1;
int nr = n - r;
for (int i = n; i > r; i--) {
num *= i;
while (nr > 1 && num % nr == 0) {
num /= nr--;
}
}
while (nr > 1)
num /= nr--;
return num;
}