Recursive Equations with Time Complexity [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am really struggling with this recursive question. Can anyone help me solve the recurrence T(n) = 5T (n/5)+5 with the base condition T(1) = 0 via closed-form formula? It is given that n = 5^m with the integer m = log5 n.

It will be sufficient to compute T(5n) for n >= 0. For all other values of x, T(x) will equal T(y) where y is the largest power of 5 smaller than x, since the calculations are the same. (I'm assuming that when you write n/5 you mean integer division, i.e. floor(n/5).)
Then:
T(50) = 0
T(51) = 5 * 0 + 5 = 5
T(52) = 5 * 5 + 5 = 52 + 51
T(53) = 5 * (5 * 5 + 5) + 5 = 53 + 52 + 51
... which leads to:
T(5n) = 5n + 5n-1 + ... + 52 + 51
which, using a high-school algebra formula (sum of a geometric series), is
T(5n) = (5n+1 - 5) / 4
If you're thinking about time complexity, notice that T(x) will always be less than or equal to 5x / 4. And since we don't worry about constant factors when expressing things in O-notation, this essentially means T(x) = O(x).

A non constructive way to solve this: looking a bit at the formula one guesses that T(5m) = (5m+1-5)/4. This can be shown by induction:
it is correct for m=0: T(1) = 0
assuming it is correct for m we show it for m+1: T(5m+1) = T(5*5m) = 5T(5m)+5 = 5*((5m+1-5)/4)+5 = (5m+2-25)/4+5 = (5m+2-5)/4.
Therefore it is correct for all m.

Related

Java formula to compute discount dynamically [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 days ago.
Improve this question
I need to implement an operation in my java program.
So I have the item, the price of it is 6,00€, but if the user get three of these items I can get a discount of 3,00€. So
int quantity = 3;
double discount = 3.0;
int quantityItem = 3;
double priceItem = 6.0;
double totalPrice = priceItem * quantityItem;
if(quantityItem == quantity){
totalePrice = totalePrice - discount;
}
The previous code is ok. But if I get 5 items the correct totalePrice should be:
priceItem (6€) * quantity (5) = 30€ - 3.0€ (discount) = 27€.
If I get 6 items the correct totalPrice should be:
priceItem (6€) * quantity (6) = 36€ - 6.0€ (discount) = 30€.
How can I write the code to make this dynamically ?
To identify the number of "groups of 3" of something bought, you need to divide the number bought by 3. Specifically, you want to use integer division, which basically means "divide by 3, then ignore the decimal part". So
1 / 3 = 0
2 / 3 = 0
3 / 3 = 1
4 / 3 = 1
5 / 3 = 1
6 / 3 = 2
...
In Java, division between two integers performs integer division by default, so you can calculate the number of "groups of 3" like so.
int discountQuantity = quantity / quantityItem;
Then you apply the discount that many times.
double totalPrice = priceItem * quantityItem - discountQuantity * discount;

Find Maximum GCD sum without repetition in an array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have an array of even number of elements, I have to select n/2( n=array size), pairs and calculate their GCD such that the sum of their GCD is max, given once we use those elements from array, we cannot use them again.
Example1: `
Input : 8 24 12 16
Output : 20
Explanation: We select two pairs (8,16) and (12,24) as sum of their GCD is maximum.
If we choose some other pairs, say (8,12) and (24,16), sum of their GCD will be 4+4 =8.
Example 2:
Input : 12 10 36 25 36 16
Output: 45
Explanation: We select the following 3 pairs : (36,36), (10,25) and (12,16) as the sum of their GCD is
36+5+4 = 45.
Our Approach:
for i in range(0,n):
max = 0;
for j in range(i+1,n):
temp = gcd(a[i], a[j]) // standard func to find GCD
if(max<temp):
store i and j
store max gcd every time and finally make a[i] and a[j] =0 to mark the visited elements
Edited
Constraint: max number of elements = 20, a[i]< 10^9.
Can you suggest an algorithm to optimally satisfy the above testcases in the least time complexity?
Because my approach is failing on multiple testcases.
This is a comment but I am not allowed to post comment yet.
It is not good to solve this problem by looking for the largest gcd.
Take [8,9,24,36], the largest gcd is gcd(24,36) = 12, that will get you gcd(24,36) + gcd(8,9) = 12+1 =13.
However, the largest sum is given by gcd(8,24) + gcd(9,36) = 8+9 = 17.

Can someone please explain this recursion question the problem is solved? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
Given base and n that are both 1 or more, compute recursively (no
loops) the value of base to the n power, so powerN(3, 2) is 9 (3
squared).
The answer is
public int powerN(int base, int n) {
if(n == 1)
return base;
return base * powerN(base, n - 1);
}
I am confused because when I look at this because is this not saying multiply the base against the returned number of powerN()?
powerN(3,3);
= 3
= 3*powerN(base, n-1) = 6
= 3*powerN(base, n-1) = 9
Which would multiply 9*6*3?
I do not see why we would have to multiply the base by the function?
Should the method not just return the answer as the base never changes and once n==1 the base case executes
Let's calculate powerN(3,3) as you did.
powerN(3,3) = 3 * powerN(3,2)
3 * powerN(3,2) = 3 * 3 * powerN(3,1)
3 * 3 * powerN(3,1) = 3 * 3 * 3 = 27
So it was just wrong the way you calculated.
Let's say we have powerN(2,3)
powerN(2,3):
call#1> return 2*powerN(2,3-1)
call#2> return 2*powerN(2,2-1)
call#3> (n==1)return 2*1
So,
call#3 returns 2 to call#2 [call#2 is now returning 2*2=4]
call#2 returns 4 to call#1 [call#1 is now returning 2*4=8]
That's it.

Optimal algorithm for smallest possible number evenly divisible by all numbers below X [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
This is my attempt to find a well performing algorithm for problem 5 Project Euler - find the smalles possible number that is evenly divisible x and all the numbers below x.
I've tried using one loop make #s and another to test if that number is evenly divisible by x and all #s below x
System.out.println("This program finds the smallest positive "
+ "number that");
System.out.println("is evenly divisible by all of the numbers "
+ "from 1 to N.");
System.out.print("Enter N: ");
int N=kb.nextInt();
long bigN=1;
for(int i=1;i<=N;i++){
bigN=bigN*i;
/*bigN serves as a definite end for our loop
when trying to find all #s divisible from 1 to n
*/
}
long smallestAnswer=bigN;
int count=0;
for(long i=1;i<=bigN;i++){//# being tested
for(int j=1;j<=N;j++){//test
if(i%j==0){
count++;
}
if(count==N && i<smallestAnswer){
smallestAnswer=i;//should catch only the first/smallest answer
break;
}
}
count=0;
}
System.out.printf("\nThe smallest # evenly divisible by all of the "
+ "numbers from 1 to N");
System.out.printf("\nis %,d\n",smallestAnswer);
}
The code works. No run/compileTime errors. It's just far too slow. If the user enters a # bigger than 11, the code just freezes basically
You are using a brute-force algorithm. Challenges, like found on Project Euler, are more often challenges to find the right algorithm, not merely challenges to write the code.
The challenge here is to find the least common multiple (see Wikipedia), of all the numbers from 1 to X.
Example: If X is 10, one way to solve it is to identify the divisors:
1 = 1
2 = 2
3 = 3
4 = 2^2
5 = 5
6 = 2 * 3
7 = 7
8 = 2^3
9 = 3^2
10 = 2 * 5
The divisors for the least common multiple is therefore:
1 * 2^3 * 3^2 * 5 * 7 = 1 * 8 * 9 * 5 * 7 = 2520
Since this is a challenge for you to solve, I'll leave the coding to you.
I'm not quite sure why you're struggling with performance.
$ date && X 20 && date
Tue Jun 25 13:18:13 CDT 2019
N: 20
232792560 is divisible by all numbers 1 to 20
Tue Jun 25 13:18:16 CDT 2019
3 seconds for N == 20.
You are doing extra math for each number you check -- a LOT of extra math. Instead of doing the check for each number 1 to N, first, you could do from 2 to N, as all numbers are divisible by 1. But more importantly, you're doing ALL even if one fails. If you turn that portion around, breaking out of your "does this number work" code as soon as a modulus check fails. On N=20, this will save you 18 checks on all odd numbers.
You could also gain more improvements. The number must be even. So if n>1, you could start at 2 and increment by 2 instead of one. If n>=3, you could actually start at 6 and increment by 6, saving a LOT of math. And if n>=4, you could start at 12 and increment by 12.
For reference, here is my implementation.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char **argv) {
int n = atoi(argv[1]);
long trying = 1;
bool found = false;
while(!found) {
found = true;
++trying;
for (long checkDivide = 2; checkDivide <= n; +checkDivide) {
if (trying % checkDivide != 0) {
found = false;
break;
}
}
}
printf("%ld is divisible by all numbers 1 to %d\n", trying, n);
return 0;
}
I skipped asking for input and just put the value on the command line.
Note that reversing the check also is probably more efficient. That is, start checking at n and work down to 2. X % 20 is going to fail more often than X % 2. I'm not using sufficient time check resolution to be sure how much more efficient it is.

new to java: what are the bits between i and j in 10000000000 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have been asked to solved the following problem:
You are given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits between i and j in N equal to M (e.g., M becomes a substring of N located at i and starting at j).
EXAMPLE:
Input: N = 10000000000, M = 10101, i = 2, j = 6
Output: N = 10001010100
what I dont understand, is what are the bits between i and j in 10000000000 ? can you explain this for me ?
I have familarity with bitwise operations, but this has totally confused me.
From the example, looks like the bits are numbered right to left, starting at 0:
N 1 0 0 0 0 0 0 0 0 0 0
Position # 10 9 8 7 6 5 4 3 2 1 0
^ ^
j=6 i=2
\_______/
|
These are the bits
between i and j in N.
Each of the digits in N is 1 bit. Starting from the right-most as 0, count the digits and move over i digits. That's the beginning of the bits you're looking for. Do the same for j. That's the end. The substring between i and j are the bits to replace.
To set a bit you can OR it into a value.
To remove/zero a bit you can AND it out of a value.
So lets assume low i and j means the least significant bits. This is reasonable.
Now you have
N = 10000000000
j = 6 --^ ^-- i = 2
M = 10101
Start by setting bit i to the value of the least bit, bit 0, in M. 1.
To set bit 2 in N like bit 0 in M you can simply
int bitToSet = 1<<i; // value of least bit i
if ( M % 2 == 1 ) // least bit is set
N = N | bitToSet;
else
N = N | ~bitToSet; // negate to remove
M /= 2; // move up to next higher bit
Rinse and repeat to j.

Categories