class Generate // to print all numbers from 1000 to 9999 whose digits are in ascending order
{
private boolean order(int n, int i) // checks if the digits of given number are in ascending order
{
if (n == 0) return true;
if (n % 10 < i) return (order(n / 10, n % 10));
return false;
}
void show(int n) // recursive function to generate numbers from 1000 to 9999
{
if (n > 9999) // base case for recursor
System.out.print("");
else
{
if (order(n, 10)) // if digits are in ascending order, prints the number
System.out.println(n);
show(n + 1); // recursive call
}
}
}
The above code was supposed to print all numbers from 1000 to 9999. Code compiled and run but received a runtime exception: java.lang.StackOverflowError: null. Would a try-catch block fix my problem? This is my first time posting here hence I am not familiar with the question etiquette, please correct me if I'm wrong.
Java (and most of the programming languages) provides finite number of memory slot to be used for stack-frame, when your program/code reaches that limit it throws the StackOverFlow error.
In your case, your program reaches that limit before coming to base condition.
My take on this -
Do not use Recursive approach until and unless you are splitting your problem into acceptable sub-problems.
this shows you how you can increase stack-frame-size to be used by the JVM for a particular thread.
In my opinion StackOverFlow is due to double recursion i.e both show(), and order() are recursive... I think show() does not need to be recursive. modified your code as under:
public static void main(String[] args) {
/
new Generate().show(); // added main function to get output or exception.. you can omit it and call Generate().show() at the point u require.
}
}
class Generate//to print all numbers from 1000 to 9999 whose digits are in ascending order
{
private boolean order(int n, int i)//checks if the digits of given number are in ascending order
{
if(n==0)
return true;
if(n%10<i)
return(order(n/10,n%10));
return false;
}
void show()//function to generate numbers from 1000 to 9999 - WITHOUT Recursion
{
int n = 1000;
while(n<=9999)
{ if(order(n,10))//if digits are in ascending order, prints the number
System.out.println(n);
n++;
}
}
}
and here is output: (showing first 7 iterations only)
1234
1235
1236
1237
1238
1239
1245 .......
Related
I'm trying to make a program where a method takes a single positive integer as its argument and returns the number of factors of 2 in the number, an example being numbers that are twice an odd number returns one, numbers that are four times an odd returns two, etc.
public int twos(int n){
int twos=0;
if(n%2!=0){
return(0);
}
if(n/3>=1){
System.out.println("\n"+n);;
return twos(n/3)+1;
}
else{
return(0);
}
// return(twos);
}
The code I have above only works for the integers 6 and 12, but not for all integers. How would you make this work for all digits?
While creating a recursive implementation, you need always define Base case and Recursive case (every recursive method would contain these two parts, either implicitly or explicitly).
Base case - represents a condition (or group of conditions) under which recursion should terminate. The result for base case is trivial and known in advance. For this task, Base case is when the number is not even, return value is 0.
Recursive case - is the place where the main logic of the recursive method resides and where recursive calls are made. In the Recursive case we need to perform a recursive call with n / 2 passed as an argument, and return the result of this call +1 (because the received argument is even, and we need to add one power of 2 to the total count).
That's how implementation might look like:
public int twos(int n) {
if (n % 2 != 0) return 0; // base case
// recursive case
return 1 + twos(n / 2);
}
Or if you would use a so-called ternary operator (which is an equivalent of if-else) it can be written as:
public int twos(int n) {
return n % 2 != 0 ? 0 : 1 + twos(n / 2);
}
Usage example (in order to be able to call twos() from the main() add static modifier to it):
public static void main(String[] args) {
System.out.println(twos(2));
System.out.println(twos(4));
System.out.println(twos(12));
System.out.println(twos(38));
System.out.println(twos(128));
}
Output:
1 // 2
2 // 4
2 // 12
1 // 38
7 // 128
I have to check whether a number is an Armstrong number or not, using a recursive method.
public class ArmStrong {
public static void main(String[] args) {
System.out.println(isArm(407, 0, 0));
}
static boolean isArm(int n,int last,int sum) {
if (n <= 0 ) {
if (sum == n) {
return true;
} else {
return false;
}
}
return isArm(n / 10, n % 10,sum + last * last * last);
}
}
When I debug, in the last call of isArm when n is 4, the base statement is skipped.
Your code will instantly jump to the answer (if (n <= 0)) before applying the cube of the last digit.
For example, trivially, let's try 9, which obviously isn't armstrong.
Your code will first check if 9 is 0 - it's not. So, we recurse, which will go with self(0, 9, 0+0). The next run is supposed to then recurse once more, so that the sum + last*last*last can actually get some cubing done. But it'll never get there - n is 0, so, you jump into the if.
As your variable name kinda gives away last is referring to the digit that the previous run lopped off, and yet you aren't cubing it.
The solution is to simply get the cubing in before checking if n is null:
The first thing your method should do is
sum += last*last*last;
Then, the second problem shows up: This correctly calculates your sum to be 407, but you check this against n, which is, obviously, 0 - you are 'destroying' n as you go through. One trivial way to solve that is to pass the original n, unmolested, through, as a 4th parameter. I'll leave that as an exercise for the reader.
Although #rzwitserloot has pointed at an issue which is rooted in the way you've designed the recursion, the problem is still not resolved.
Yes, the recursive calls go "one step ahead" the calculation of the sum of digits, which happens in the Recursive case, and the left-most digit of the given number doesn't contribute the sum, since n is zero and recursion hits the Base case. That's true, but it's not the only thing you need to fix.
Here's the definition of so-called Narcissistic numbers (aka Armstrong numbers):
In number theory, a narcissistic number (also known as a pluperfect digital invariant (PPDI), an Armstrong number (after Michael F. Armstrong)or a plus perfect number) in a given number base b b is a number that is the sum of its own digits each raised to the power of the number of digits.
Therefore, a method with hard-coded power of 3 is capable to validate only three-digit Armstrong numbers. There are only few of them, for other valid Armstrong numbers it would produce an incorrect result.
So, before calculating the sum you need to know the numbers of digits there's no workaround. If you want to address the problem only by using Recursion, the numbers of digits can be also calculated recursively. If we take this route, the solution boils down to implementing two recursive methods.
That's how it might look like:
public static boolean isArm(int n) {
return n == getSum(n, getDigitCount(n));
}
public static int getSum(int n, int power) {
return n == 0 ? 0 : (int) Math.pow(n % 10, power) + getSum(n / 10, power);
}
public static int getDigitCount(int n) {
return n == 0 ? 0 : 1 + getDigitCount(n / 10);
}
main()
public static void main(String[] args) {
System.out.println(isArm(10)); // is not an Armstrong number
System.out.println(isArm(21)); // is not an Armstrong number
System.out.println(isArm(407)); // is an Armstrong number
System.out.println(isArm(153)); // is an Armstrong number
System.out.println(isArm(9)); // is an Armstrong number
}
Output:
false // 10
false // 21
true // 407
true // 153
true // 9
I started learning recursions and it seemed like I understood how they work. But as soon as I tried to pass a quiz, I found that I actually didn't. Here is some code from latest task I tried to pass. Here I tried to find a factorial. To do this I have to multiply all the numbers from 1 till the N number.
Here is some code:
public class App {
public static void main(String[] args) {
f(1);
}
private static void f(int n) {
System.out.print(n);
if (n < 7) {
f(2 * n);
}
}
}
I tried to multiply these nums and to multiply the product for 2. But I failed when saw that the result isn't 5040. The actual output is 1248. Maybe I was doing actions in wrong order?
Let's take a look how to multiply numbers from 1 to n using recurrency:
public static void main(String[] args) {
long result = mulFromOneTo(30);
}
private long mulFromOneTo(int number) {
if (number == 1) {
return 1;
} else {
return number * mulFromOneTo(number - 1);
}
}
I don't want to give you completely prepared source code - but I hope it'll help you a bit.
Andrezej's answer is a good top-down approach. Let's have a look at why your code doesn't work:
If we summarize what f is doing, it prints n and it will call itself recursively with 2 * n when n is less than 7.
So in your example, you first call f(1) so it prints 1 and calls f(2), which prints 2 and calls f(4), prints 4, calls f(8), prints 8 and stop.
So your function is not printing the number 1248, but it's printing 1, 2, 4 and 8 independently.
The total computation done by your recursive function is (2 * (2 * (2 * 1)))
Im trying to implement a function that returns a natural number which is the sum of the digits within an entered natural number. I just keep on getting an infinite loop. I know i have to return the recursive call but i cant figure this out. Here is what i have so far:
private static NaturalNumber sumOfDigits(NaturalNumber n) {
NaturalNumber zero = new NaturalNumber2(0);
if (n.compareTo(zero) == 0) {
return zero;
} else {
NaturalNumber z = new NaturalNumber2(n.divideBy10());
n.divideBy10();
z.add(sumOfDigits(n));
// return ___;
}
}
What am i supposed to return? Returning z doesn't work
You are making the recursive call with n, the same number that was passed into your procedure. If you strip off a digit for z, then the recursive call has to be made with the rest of the digits.
You can strip off a digit with mod 10 and then get the rest of the digits by divide by 10. If you were using ints it would be:
return (n % 10) + sumOfDigits(n / 10);
z.add(sumOfDigits(n)); should be z.add(sumOfDigits(n.divideBy10()));. The main point is in the recursion you only want to process the remainder of the answer, not the whole problem.
How can I write a function that takes an array of integers and returns true if there exists a pair of numbers whose product is even?
What are the properties of even integers? And of course, how do you write this function in Java? Also, maybe a short explanation of how you went about formulating an algorithm for the actual implementation. This is from the book however I am only trying to learn the explanation.
A pair of integers will always produce an even product if at least 1 of them is even. It can't produce an even product if both are odd. Therefore, you just need to check if there is at least 1 even number in the array.
This is a mathematical concept and known fact that, any even number multiplied by any other number is an even number. And so, all you have to do is return true if any one number in the array is an even number.
As others have pointed out, just having one even number is enough.
You only have to iterate until you find an even number, or the end of the array in which case there is no even number in it.
public static boolean hasEvenNumber(int[] vals) {
int i = 0;
while(i < vals.length) {
if(vals[i] % 2 == 0) {
return true;
}
i++;
}
return false;
}
Algorithm is quite simple .
You need a single even number should present in the array of integers .
Because multiple of even number with odd/even number results in a even number . So you can return true if there is an even number .
In java ,
public static int has_even_pair(int [ ] values,int n) //method definition to find sum
{
int i;
for(i=0; i< n; i++)
{
if (values[i] % 2 == 0 )
{
return 1;
}
}
return 0 ;
}