Can anybody help me programming the next problem (taken from Codingbat- Recursion1- count7)
Given a non-negative int n, return the count of the occurrences of 7 as a digit, so for example 717 yields 2. (no loops). Note that mod (%) by 10 yields the rightmost digit (126 % 10 is 6), while divide (/) by 10 removes the rightmost digit (126 / 10 is 12).
count7(717) → 2
count7(7) → 1
count7(123) → 0
There are some solutions which includes number of "returns".
I would like to program the problem with only 1 "return".
Well here's the solution I wrote and let's see how to do it with only one return
public int count7(int n)
{
int c = 0;
if (7 > n)
{
return 0;
}
else
{
if ( 7 == n % 10)
{
c = 1;
}
else
{
c = 0;
}
}
return c + count7(n / 10);
}
the same with only one return
public int count7(int n)
{
return (7 > n) ? 0 : ( ( 7 == n % 10) ? 1 + count7(n / 10) : 0 + count7(n / 10));
}
public int count7(int n) {
int counter = 0;
if( n % 10 == 7) counter++;
if( n / 10 == 0) return counter;
return counter + count7(n/10);
}
Sure PFB my solution in JAVA for the same
public int count7(int n) {
if((n / 10 == 0) && !(n % 10 == 7)) //First BASE CASE when the left most digit is 7 return 1
return 0;
else if((n / 10 == 0) && (n % 10 == 7)) //Second BASE CASE when the left most digit is 7 return 0
return 1;
else if((n % 10 == 7))
//if the number having 2 digits then test the rightmost digit and trigger recursion trimming it there
return 1 + count7(n / 10);
return count7(n / 10);
}
public int count7(int n) {
if(n == 0)
return 0;
else{
if(n%10 ==7)
return 1+count7(n/10);
return 0+count7(n/10);
}
}
public static int count7(int n){
if(n == 7)
return 1;
else if(n > 9){
int a = count7(n%10);
int b = count7(n/10);
return a + b;
}else
return 0;
}
public int count7(int n)
{
if(n==0)return 0;
if(n%10==7)return 1+count7(n/10);
else return count7(n/10);
}
public int count7(int n) {
if (n != 7 && n < 10) return 0;
else if (n == 7) return 1;
else if (n%10 == 7) return count7(n/10) + 1 ;
else return count7(n/10);
}
public int count7(int n){
if(n < 7)
return 0;
else if(n % 10 == 7)
return 1 + count7(n / 10);
else
return count7(n / 10);
}
the first if-statement is the base case that we would want to terminate on. The second checks to see if the rightmost digit is 7. If it is, chop the rightmost digit off and try again. When the recursive calls terminate and and values start getting returned up the chain, add 1 to include this successful check. In the case that neither of the above statements are true, chop off the rightmost digit and try again.
I know this is 2 years old, but hope this is a bit more readable and intuitive, and thus helpful.
Here is how I did it.
public int count7(int n) {
return (n==0?0:(count7(n/10)+(n%10==7?1:0)));
}
Recursion goes to 0 and it returns 0, when it comes back out checks if each number is 7, returns 1 if it is else 0. It continues adding those until all the way out.
Using one return will likely make it harder to read. If you are counting occurrences in recursion, an easy formula is to create a base case to terminate on, then provide an incremental return, and finally a return that will aid in reaching the base case without incrementing. For example..
public int count7(int n) {
if(n == 0) return 0;
if(n % 10 == 7) return 1 + count7(n / 10);
return count7(n / 10);
}
Using a one liner return like the one below in my opinion is harder to read or update because of the double ternary..
public int count7(int n)
{
return (n == 0) ? 0 : (n % 10 == 7) ? 1 + count7(n / 10) : count7(n / 10);
}
My solution works backwards from the nth digit to the first digit, by taking the modulus of the input. we add the number of sevens found into the return, for the final output.
Then checking whether the input is less than 7 can be the next step. If the input is less than 7 then there have never been any 7s in the input to begin with.
public int count7(int n) {
int sevens_found = 0;
if( n % 10 == 7 ) sevens_found ++;
return ( n < 7) ? 0 : ( n % 10 == 7 ) ? sevens_found + count7 ( n / 10 ) : count7 ( n / 10 );
}
#include<bits/stdc++.h>
using namespace std;
int count_occurences(int k){
int r;
if(k==0){
return 0;
}
r = k%10;
k = k/10;
if(r!=7){
return count_occurences(k);
}
return 1+count_occurences(k);
}
int main()
{
int x;
cin>>x;
cout<<" "<<count_occurences(x);
return 0;
}
public int count7(int n) {
int length = 0;
int counter = 0;
if ((n / 10) * 10 != n || (n / 10) != 0) {
if (n % 10 != 7) {
counter++;
}
length += 1 + count7(n / 10);
}
return length - counter;
}
The base case of n == 0 just "breaks" us out of the recursive loop, the n % 10 == 7 allows us to actually count the amount of 7s in an integer, and the return statement iterates through the given argument.
public int count7(int n) {
if (n == 0)
return 0;
if (n % 10 == 7)
return 1 + count7(n / 10);
return count7(n / 10);
}
public int count7(int n)
{
return occurrencesCounting(n, 0);
}
private int occurrencesCounting(int n, int count)
{
int counter = n % 10 == 7 ? count + 1 : count;
if (n / 10 == 0)
{
return counter;
}
return occurrencesCounting(n / 10, counter );
}
Related
I am working on this problem where I am supposed to use loops to find whether two numbers share a digit. The code I wrote does not return true if the shared digit is the first digit of a number. I can not find the bug in my code or any other solution. Please help!
public static boolean hasSharedDigit(int firstNumber, int secondNumber) {
if ((firstNumber < 10 || firstNumber > 99) || (secondNumber < 10 || secondNumber > 99)) {
return false;
}
int testFirstNumber = firstNumber;
int testSecondNumber = secondNumber;
while (testFirstNumber != 0) {
while (testSecondNumber != 0) {
if ((testFirstNumber % 10) == (testSecondNumber % 10)) {
return true;
}
testSecondNumber /= 10;
}
testFirstNumber /= 10;
}
return false;
}
You should reset testSecondNumber before the next testFirstNummber loop.
In your code, the inner loop is called only once, because testSecondNumber goes to 0 and is not reset.
The right solution is:
public static boolean hasSharedDigit(int firstNumber, int secondNumber) {
if ((firstNumber < 10 || firstNumber > 99) || (secondNumber < 10 || secondNumber > 99)) {
return false;
}
int testFirstNumber = firstNumber;
while (testFirstNumber != 0) {
int testSecondNumber = secondNumber;
while (testSecondNumber != 0) {
if ((testFirstNumber % 10) == (testSecondNumber % 10)) {
return true;
}
testSecondNumber /= 10;
}
testFirstNumber /= 10;
}
return false;
}
It seems that offered solution checks only 2-digit numbers so the performance is not the issue in this case and using nested loop should not have any serious impact.
However, if a common digit needs to be found for any integer numbers (not only positive), it would be better to use a small array to count digits in the first number and then verify if a digit from the 2nd number is present in this array without using nested loops.
static boolean commonDigits(int xx, int yy) {
// handle negative values
int x = Math.abs(xx);
int y = Math.abs(yy);
int[] digits = new int[10];
if (x == 0) { // handle 0
digits[0]++;
}
while (x > 0) {
digits[x % 10]++;
x /= 10;
}
// check for 0
if (y == 0 && digits[0] > 0) {
return true;
}
while (y > 0) {
if (digits[y % 10] > 0) {
return true;
}
y /= 10;
}
return false;
}
A shorter version based on converting a number into stream of characters via conversion to String may look like this:
static boolean commonDigitsStream(int x, int y) {
int[] digits = new int[10];
Integer.toString(Math.abs(x))
.chars()
.map(i -> i - '0')
.forEach(i -> digits[i]++);
return Integer.toString(Math.abs(y))
.chars()
.map(i -> i - '0')
.anyMatch(i -> digits[i] > 0);
}
I am looking for a Method that removes all 9's from an Integer:
public int noNine(int i){
int res = 0;
return res;
}
Examples:
noNine(0)->0
noNine(1991)->11
noNine(99)->0
noNine(19293949)->1234
And so on.
No Strings are allowed and no external Methods.
Can you help me?
Thanks!
Eddie Texas
int removeNine(int n)
{
int result = 0;
int mul = 1;
while(n > 0)
{
//check if current digit is 9. if 9 then do nothing
if(n % 10 == 9)
{
n = n / 10;
continue;
}
else
{
//if not 9 then add this to result after multiplying by current mul
result += mul * (n % 10);
//update mul so that the next digit is added according to power of 10
mul = mul * 10;
}
n = n / 10;
}
return result;
}
You could solve this problem in multiple ways
Using a for loop to loop through each character
Using recursion
I'm going to elaborate on the second technique:
Using this technique, you could solve the problem using integers or strings. I'm going to be using intergers as you said it is a required aspect:
Get the last digit using % 10
Remove it if it is a 9
Recursively check each digit (*Remember to have a base case!)
Return final value
public int noNine(int i){
if(i < 10){
if(i == 9)
return 0;
else
return i;
}
int lastDigit = i % 10;
if(lastDigit == 9)
return noNine(i / 10);
else
return noNine(i / 10)*10+lastDigit;
}
The key takeaway here is that: n % 10 = last digit of n and n / 10 = all previous digits of n. This happens due to integer division in Java!
You could do it like this:
public int removeNines(int n) {
int returnValue = 0, multiplier = 1;
while (n > 0) {
if (n%10 != 9) {
returnValue += (n%10) * multiplier;
multiplier *= 10;
}
n /= 10;
}
return returnValue;
}
This loops through all digits and if it is not a 9 it adds it to the output. Tested here and works
Here's my version written in a slightly different manner than ritratt just in case you are not understanding his version:
public int noNines(int num) {
int multiplier = 0;
int result = 0;
while (num > 0) {
int digit = num % 10;
System.out.println("digit=" + digit);
if (digit == 9) {
//ignore
} else {
System.out.println("Adding " + (digit * (int)Math.pow(10, multiplier)));
result += digit * (int)Math.pow(10, multiplier);
multiplier++;
}
num = num / 10;
}
return result;
}
I left console output so you can see the method in action.
I want to find the sum of numbers that is divisible by x using recursive method
Ex if n= 10, x=3, the code should return sum of 3+6+9
Write a recursive method sumDivByX(n, x), which finds the sum of all
numbers from 0 to n that are divisible by x.
I asked my teacher about it and he told me "Firstly, total should be global. You should return 0 if n or x == 0. I only care if n is divisible by x. So I only add n to total (total+=n) if (n%x==0) otherwise do nothing. And do recursion sumDivByX(n-1,x) and return total as usual." I tried to correct it.
public static int sumDivByX(int n, int x) {
int total = 0;
if (n == 0 || x == 0) {
return -1;
}
if (n % x >= 1) {
return total = 0;
} else if (n % x == 0) {
return total += n;
}
return total + sumDivByX(n - 1, x);
}
When I run the program I get 0.
Eliminate the returns inside your second and third if statements
public static int sumDivByX(int n, int x) {
int total = 0;
if (n == 0 || x == 0) {
return 0;
}
if (n % x >= 1) {
total = 0;
} else if (n % x == 0) {
total += n;
}
return total + sumDivByX(n - 1, x);
}
For a cuter, more compact version
public static int sumDivByX(int n, int x) {
if (n == 0 || x == 0) {
return 0;
}
return (n % x == 0 ? n : 0) + sumDivByX(n - 1, x);
}
Note - depending on the semantics you intend, you might want to have separate checks for x<=0 (possibly and error?) and n==0 (base case).
Step through your code and you'll see that it never recurses when n ==10 and x==3, since (10 % 3 == 1)
When a method gets to a "return" statement it ends, in your case at the second if.
Your total is initialized by 0 everytime the method runs, so you should consider making it global.
Your method generates an exception if you try to use negative numbers as paramethers
Try this:
int total=0;
public static int subDivByX(int n, int X) {
if (n>0 && x>0) {
if (n%x==0){
total += n;
}
return sumDivByX(n-1,x);
}
else return -1;
}
This seems to work
private static int sumDivByX(int n,int x) {
if (n < x || x < 1 ) {
return 0;
}
int d = n/x;
return (x * d) + sumDivByX(n - x , x);
}
Recursion could cause a stackoverflow.
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10 001st prime number?
My solution:
public class Prime_Number {
public static boolean isPrime(long n) {
if ((n > 2 && n % 2 == 0) || (n > 3 && n % 3 == 0) || (n > 5 && n % 5 == 0) || n == 0 || n == 1) {
return false;
}
return true;
}
public static void main(String[] args) {
int count = 0;
int prime = 0;
while (prime <= 10001) {
if (isPrime(count) == true) {
prime++;
if (prime == 10001) {
System.out.println(count + " is a prime number" + "(" + prime + ")");
}
}
count++;
}
}
}
But it does not give a correct answer. Please help me to upgrade my code. For instance, program defines a 91 as a prime number, but it is not a prime number. How to improve it?
You need to test the number against every prime less than its square root to ensure it is prime.
You're only testing against 2,3 and 5.
Because storing all the primes is not always space-feasable, a common technique is to test for 2, and then test all odd numbers starting at 3. This requires a loop.
consider:
boolean isPrime(long n) {
if (n < 2) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
if (n < 9) return true;
if (n % 3 == 0) return false;
long max = (long)(Math.sqrt(n + 0.0)) + 1;
for (int i = 5; i <= max; i += 6) {
if (n % i == 0) return false;
if (n % (i + 2) == 0) return false;
}
return true;
}
A number p is prime if it only divides by itself and 1. You are checking only for divison by 2, 3 and 5. This is not enough. Check for every number till p / 2, or better till sqrt(p).
The following solution checks only the odd numbers to be prime, but it counts 2 as prime from the beginning:
public class A {
static int N = 10001;
private static boolean isOddPrime(long x) {
for ( int i = 3 ; i*i <= x ; i+=2 ) {
if ( x % i == 0 ) {
return false;
}
}
return true;
}
public static void main(String[] args) throws Exception {
long start = System.nanoTime();
int x;
int i = 2; // 3 is the 2nd prime number
for ( x = 3 ; ; x+=2 ) {
if ( isOddPrime(x) ) {
if ( i == N )
break;
i++;
}
}
System.out.println(x);
long stop = System.nanoTime();
System.out.println("Time: " + (stop - start) / 1000000 + " ms");
}
}
Output:
104743
Time: 61 ms
I would comment, but I just joined.
You don't have to check every number between 1 and a numbers square root for potential divisors, you just have to check all previous primes (assuming you start at 1 and iterate up), as any other divisor that is not prime will itself be divisible by a prime of a lower value. the higher the number of primes, the more checks against non prime numbers this saves. the example is in C# but that's more to demonstrate the concept.
//store found primes here, for checking subsequent primes
private static List<long> Primes;
private static bool IsPrime(long number)
{
//no number will have a larger divisor withou some smaller divisor
var maxPrime = Math.Sqrt(number);
// takes the list of primes less than the square root and
// checks to see if all of that list is not evenly
// divisible into {number}
var isPrime = Primes
.TakeWhile(prime => !(prime > maxPrime))
.All(prime => number % prime != 0);
if (isPrime)
Primes.Add(number);
return isPrime;
}
private static long GetNthPrime(int n)
{
//reset primes list to prevent persistence
Primes = new List<long> { 2, 3, 5, 7 };
//prime in starting set
if (Primes.Count >= n)
{
return Primes[n - 1];
}
//iterate by 6 to avoid all divisiors of 2 and 3
// (also have to check i + 2 for this to work)
// similar to incrementing by 2 but skips every third increment
// starting with the second, as that is divisible by 3
for (long i = 11; i < long.MaxValue; i += 6)
{
// only check count if is prime
if ((IsPrime(i) && Primes.Count >= n) || (IsPrime(i + 2) && Primes.Count >= n))
{
break;
};
}
//return end of list
return Primes[n - 1];
}
Im stuck with the below problem.
Problem Statement:
Given a non-negative int n, return the count of the occurrences of 8 as a digit, except that an 8 with another 8 immediately to its left counts double, so 8818 yields 4.
Note: mod (%) by 10 yields the rightmost digit (126 % 10 is 6), while divide (/) by 10 removes the rightmost digit (126 / 10 is 12).
The above problem has to be solved without using Recursion and without the usage of any formulas.
The function signature is public int count8(int n)
Examples are:
count8(8) → 1
count8(818) → 2
count8(8818) → 4
I got this problem from one of the Programming Forums. I dont know how to start with this problem, I want to solve it, but I am really confused on where to begin.
the way to do this using the mod operator is to use %10 to get the last digit and /10 to remove the last digit in essence iterating through the number. If you %10 and get an 8 you can incremement a count, you can also keep a flag that lets you know if the last digit you saw was an 8 or not so you know how to increment your count
boolean lastWas8 = false;
int count = 0;
while (n != 0)
{
int digit = n % 10;
if (digit == 8)
{
if (lastWas8) count++;
count++;
lastWas8 = true;
}
else lastWas8 = false;
n/=10;
}
return count;
As none of the answers until now was recursive, here is my try at a recursive solution.
public int count8(int n) {
return
n <= 0 ? 0 :
( n%100 == 88 ? 2 :
n%10 == 8 ? 1 : 0)
+ count8(n/10);
}
Here the same program in a longer version:
public int count8(int n) {
Numbers without digits have no eights in them.
if(n <= 0) {
return 0;
}
Count the last digit:
int last;
If the last digit is an 8 and the digit before, too, count the last 8 doubled:
if(n % 100 == 88) {
last = 2;
}
If the last digit is an 8 (and the one before not), count it once.
else if(n % 10 == 8) {
last = 1;
}
Otherwise, the last digit is not an 8:
else {
last = 0;
}
The number without the last digit:
int withoutLast = n/10;
The number of eights in n is the number of eights in the last digit + the number of eights in the number without its last digit:
return last + count8(withoutLast);
}
Since I misread the question, here a iterative version of the same algorithm:
public int count8(int n) {
int count = 0;
while(n > 0) {
count += ( n%100 == 88 ? 2 : n%10 == 8 ? 1 : 0);
n/= 10;
}
return count;
}
Or with a for-loop:
public int count8(int n) {
int count = 0;
for( ; n > 0; n/=10) {
count += ( n%100 == 88 ? 2 : n%10 == 8 ? 1 : 0);
}
return count;
}
I saw that all the other solutions have used mods or divs but you could also just process it as a String I guess (I don't see anything in the question that says you can't despite the hints they give you). This is just an alternative solution.
I apologise in advance if I have missed some of the "rules" around the answer to this question but here we go anyway:
private int count8(int n) {
String nString = Integer.toString(n);
boolean isPrevChar8 = false;
int total = 0;
for (int i = 0; i < nString.length(); i++) {
char nextChar = nString.charAt(i);
if (nextChar == '8') {
total += (isPrevChar8 ? 2 : 1);
isPrevChar8 = true;
} else {
isPrevChar8 = false;
}
}
return total;
}
try this :
public static int count8(int num) {
int count=0;
boolean doubl = false;
while(true) {
int n = num%10;
num = num/10;
if(n==8) {
if(doubl) {
count = count+2;
} else {
count++;
}
doubl=true;
}
else {
doubl=false;
}
if(num == 0) break;
}
return count;
}
EDIT: Check this out for no recursion and no formula.
public static int count8(int num) {
int count=0;
boolean doubl = false;
String str = "" + num;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '8') {
if (doubl) {
count = count + 2;
} else {
count++;
}
doubl = true;
} else {
doubl = false;
}
}
return count;
}
Here is my solution:
public int count8(int n) {
int count = 0;
if(n == 0)
return 0;
if(n % 100 == 88)
{
count = 3;
return count + count8(n/100);
}
else if(n % 10 == 8)
{
count++;
return count + count8(n/10);
}
else
return count8(n/10);
}
However, for the case: count8(88888) → 9, I get 7, and I can't figure out why.
What I also find strange is that a double 8 yields 3 so for the case: count8(8818) → 4 instead of 5, which is what I thought it would be. Hence, why I have count = 3 for the (n % 100 == 88)
Here is my code . The solution to this problem is very simple . I have done it with pure recursion . :)
public int count8(int n) {
if (n==8) return 1;
if (n<10) return 0;
if (n%100==88)
return 2 + count8(n/10);
if (n%10==8)
return 1 + count8(n/10);
return count8(n/10);
}
The catch of the problem is that when a pair of 88 comes total count = 1 + 2 ; 1 for 8 at right and 2 for 8 at left because the previous digit(which is digit at its adjacent right) was also 8 .
So for 88 the total occurances of 8 is equal to 3. For implementing this logic (n%100 ==88) condition is added .
This is the another recursion technique which I have used to solve this problem :-
public int count8(int n) {
int a,b;
if(n==0)
return 0;
a=n%10;
b=(n/10)%10;
if(a==8&&b==8)
return 2+count8(n/10);
else if(a==8&&b!=8)
return 1+count8(n/10);
else
return count8(n/10);
}
This code also works;
public int count8(int n) {
if(n/10 == 0 && n%10 != 8){
return 0;
}
if(n % 10 == 8 && (n/10)%10 == 8){
return 2 + count8(n/10);
}
if(n/10 == 0 && n%10 == 8){
return 1 + count8(n/10);
}
if(n % 10 != 8){
return 0 + count8(n/10);
}else{
return 1 + count8(n/10);
}
}
Here is simple solution
public int count8(int n) {
//base case if n becomes 0 then return 0
if(n==0) return 0;
//checking for two consecutive 8's in a row
if((n%10) == 8 && (n/10)%10 == 8){
return 2 + count8(n/10);
}
else if(n%10 == 8){ // there is only one 8
return 1 + count8(n/10);
}
//no 8 found
return count8(n/10);
}
Here's my solution, albeit the function names aren't nicely named, just think of them as abstract (not in the Java abstract keyword sense) functions that perform their task.
public int count8(int n) {
return g(n, 0);
}
public int g(int n, int prev) {
int rest = n/10;
int digit = n % 10;
if (rest == 0) {
return h(digit, prev);
}
int toAdd = h(digit, prev);
return toAdd + g(rest, digit);
}
public int h(int digit, int prev) {
return prev == 8 && digit == 8 ?
2 : digit == 8 ?
1 : 0;
}