Skip to content

Instantly share code, notes, and snippets.

@sbsatter
Created August 19, 2016 14:59
Show Gist options
  • Save sbsatter/418e43834053c52c2e5e5c6a83e13aba to your computer and use it in GitHub Desktop.
Save sbsatter/418e43834053c52c2e5e5c6a83e13aba to your computer and use it in GitHub Desktop.
Given a non-negative int n, compute recursively (no loops) 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 that mod (%) by 10 yields the rightmost digit (126 % 10 is 6), while divide (/) by 10 removes the rightmost digit (126 / 10 is 12). count8(8) → 1 co…
public int count8(int n) {
if(n==0)
return 0;
int rem100=n%100;
int div=n/10;
int rem=n%10;
if(rem100==88){
return 2+count8(div);
}
if(rem==8){
return 1+count8(div);
}
return count8(div);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment