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

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.

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;

Returning a random element from my custom ArrayList class? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have made my own generic version of the ArrayList class and it needs to include a method that returns a random element from the list. Here is the method I wrote:
public T randomElement() throws NoSuchElementException {
T elem;
int rand = (int) (1 + Math.random() * size);
elem = items[rand];
return elem;
}
I am using this class/method to pick a random element from my list (using characters taken from a .txt file) and I keep getting a NullPointerException error. Are there any protocols I can take to make sure that doesn't happen?
The problem in your code is the way you compute the random index rand:
int rand = (int) (1 + Math.random() * size);
Because of the 1 + the value might be equal to size which is the index of the first empy space in your list (at least I assume that).
So in some cases you return the value of an index that has not been set yet - which is null.
Change it to
int rand = (int) (Math.random() * size);
The NullPointerException could be happening because of one of two things -
items is null. Check the condition and return null.
Caller is not handling a returned null.
In any case, calculation of random index is wrong. Assume size of ArrayList is 5 and Math.random returns 0.9. In this case,
rand = (int)(1 + 0.9 * 5) = 5
This is the size of the ArrayList and will result in an out-of-bounds access. Just remove the "1+"

Recursive Equations with Time Complexity [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 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.

Java for every 6 remove 1 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm making a system.
What I want is, for every 6 items you have to buy 5 (so when the price is 5 each item, 6 items is not 30 but 25, same with 12, 18, 24 etc...)
How would I do that?
I thought it would be something like this:
if (amount % 6 == 0) {
}</code>
But that would get it one time if I'm correct.
The modulus operator won't work in this situation.
So for an efficient solution.
int numberOfItems = 17; //however many there are
int discount = numberOfItems / 6;
double priceToPay = price * (numOfItems - discount);
By having the discount as an int you won't get any rounding or decimal part after the division.
Using modulus will only give you the discount if you have 6, 12, etc. items. What about if you have 7 items? You won't get any discount (it is not divisible by 6)! So, it would be something like this:
int numOfItems = 6; //this will be different every time
//copy numOfItems because it will be modified
int temp = numOfItems;
double price = 5;
int discount = 0;
//While there are at least 6 items
while (temp >= 6) {
discount++; //discount one item
temp -= 6; //take away 6
}
//you have to pay for the number of items, but not the discounted items
double amountToPay = price * (numOfItems - discount);
This way, every time you take away 6, you don't have to pay for 1 item.

How does recursion work? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Please explain how recursion works, in the simplest way you can.
As been pointed out, recursion is when a function calls itself. Here illustrated using factorial, where factorial(5) mathematically evalutates to values 5 * 4 * 3 * 2 * 1.
public int factorial(int x) {
if (x == 1) return 1;
else return x * factorial (x - 1);
}
// Try routine
factorial(3);
Which evaluates as
factorial(1) = 1 = 1
factorial(2) = 2 * factoral(1) = 2 * 1
factorial(3) = 3 * (2 * factorial(2) * (factorial(1)) = 3 * 2 * 1
...
Usually, it's a function that calculates one result itself, and calls itself to get the rest of the results.
For example, to get all positive numbers less or equal to 3, the function says, "One result is 3, and the rest of the results are all positive numbers less or equal to 2. Let's call myself with 2 and add that result to the one I calculated."
Of course, recursive functions need to be careful to have an "end condition" or they will never return a result. In the case of this example, the end condition would be when the function is called with 0, it should just return 0.
Here's a simple example of recursive method: -
public int recur(int count) {
if (count < 10) {
return count + recur(count++);
}
return count;
}
System.out.println(recur(0)); // Invoke first time
Recursion is a method which calls itself from within itself.
Example:
public void recur(int x)
recur(10);
end
Recursion is when a function calls itself:
int factorial(int integer)
{
if (integer == 1)
return 1;
else
return (integer*(factorial(integer-1)));
}

Categories