A simber is defined as a positive integer in which any odd digit, if present, occurs an odd number of times, 1414414 is a simber. 4 is even and it appears four time and 1 is odd and it appears 3 times.
Here are my variables:
int is_simber(int n)
{
int numberOfTimes = 0, length = 1, x = n;
bool answer;
vector <int> nmbrs = vector <int>();
//get how many digits are in the integer
do
{
x /= 10;
length++;
}
while(x != 0)
//get the digits in the integer
for(int i = 0; i<length; i++)
{
nmbrs.push_back(((n/10^i) % 10);
}
//checking how many times a digit occurs and also testing to see if the digits
//meet the requirements
for(int i = 0; i<length; i++)
{
for(int j = 0; j<length; j++)
{
if (nmbrs.at(i) == nmbrs.at(j))
{
numberOfTimes++;
}
}
if (nmbrs.at(i) % 2 == 0 && numberOfTimes % 2 == 0)
{
answer = true;
}
else if(nmbrs.at(i) % 2 == 1 && numberOfTimes % 2 == 1)
{
answer = true;
}
else if(nmbrs.at(i) % 2 == 0 && numberOfTimes % 2 == 1)
{
answer = false;
break;
}
else if(nmbrs.at(i) % 2 == 1 && numberOfTimes % 2 == 0)
{
answer = false;
break;
}
}
return answer;
}
Your code has compilation errors.
^ is Binary XOR Operator. You can't expect it to produce pow(10,i). So replace nmbrs.push_back(((n/10^i) % 10); with nmbrs.push_back(((n/pow(10,i)) % 10);
When I was able to remove the compilation errors, i realized your code has logical errors too. As your given definition of simber, int is_simber(int n) should be fair and simple.
bool is_simber( int n )
{
if ( n<0 ) return false; // simber is a positive integer
int digitCount[10] = {}; // initializing all with 0;
// digitCount array holds the number of occurance of a digit
// so d[1] holds the number of times 1 occurred
// d[2] holds the number of times 2 occurred and so on ...
while( n ){
int d = n%10;
n /= 10;
digitCount[d]++;
}
// we just have to check
// any odd digit, if present, occurs an odd number of times
for( int i=1; i<=9; i= i+2) // Attention i=i+2, to iterate over just odd numbers
{
if( digitCount[i] != 0 && digitCount[i]%2 == 0 ) return false;
}
return true;
}
My definition of is_simber
inline bool is_even(int n) { return n % 2 == 0; }
bool is_simber(int n) {
if (n < 0) return false;
int digits[10] = {0};
for (; n; n /= 10) ++digits[n % 10];
for (int i = 0; i < 10; i += 2)
if (!is_even(digits[i]) && is_even(digits[i + 1])) return false;
return true;
}
LIVE DEMO
Related
The Question is:
You are given a number 'N' in the form of a string 'S', which is a palindrome. You need to find the greatest number strictly less than 'N' which is also a palindrome.
I tried to solve this question but is giving wrong answer for some test cases. can anyone help me to correct my code.
Below is my code:
string nextSmallerPalindrome(string &s)
{
int n = s.length();
string ans = "";
if(n == 1)
{
s[0]--;
return s;
}
if(s == "11")
{
return "9";
}
// For Handling odd cases
if(n % 2 != 0)
{
int idx = n / 2;
int diff = 0;
if(s[idx] == '0')
{
s[idx] = '9';
diff = 1;
}
else
{
s[idx]--;
}
idx--;
while(idx >= 0 && diff == 1)
{
if(s[idx] == '0')
{
s[idx] = '9';
idx--;
}
else
{
s[idx]--;
diff = 0;
break;
}
}
int i = 0;
while(i < n && s[i] == '0')
{
i++;
}
for(; i < n; i++)
{
ans = ans + s[i];
}
int new_n = ans.length();
int j = 0;
int k = new_n - 1;
while(j < k)
{
if(ans[j] == ans[k])
{
j++;
k--;
}
else
{
ans[k] = ans[j];
j++;
k--;
}
}
return ans;
}
else // For handling even cases
{
int idx = n / 2 - 1;
int diff = 0;
if(s[idx] == '0')
{
s[idx] = '9';
diff = 1;
}
else
{
s[idx]--;
}
idx--;
while(idx >= 0 && diff == 1)
{
if(s[idx] == '0')
{
s[idx] = '9';
idx--;
}
else
{
s[idx]--;
diff = 0;
break;
}
}
int i = 0;
while(i < n && s[i] == '0') // For ignoring Zeros from front of the string
{
i++;
}
for(; i < n; i++) //storing all the string s in new string ans after ignoring front 0
{
ans = ans + s[i];
}
int new_n = ans.length();
int j = 0;
int k = new_n - 1;
while(j < k) // checking and changing the last half into first half
{
if(ans[j] == ans[k])
{
j++;
k--;
}
else
{
ans[k] = ans[j];
j++;
k--;
}
}
return ans;
}
}
Input Format:
The first line of the input contains an integer T denoting the number of test cases.
The first and the only line of each test case contains a string 'S', denoting the number whose next smaller palindrome is to be found.
Test Case:
19
7
77
101
1001
1221
144441
3444444443
57855875
10000001
11
1
111
101
1001
11011
1110111
1190911
20002
10011001
Excuse me, but I will not fix the code. I will rather describe how I would solve it. If you like the idea, then you will have a very easy time doing it.
A palindrome has n digits. n may be pair or odd. The first n / 2 digits (rounded upwards) is a number. Get that number (cut down the digits in the second half) and subtract 1.
Look at the result and the number of its digits. If subtracting 1 decreases the number of digits of your half-number, then handle that accordingly. Should be easy if you have the right idea.
Seems like using the same expression for idx in even cases as odd cases works better.
(It still doesn't handle the case for 7, though.)
Change this:
else // For handling even cases
{
int idx = n / 2 - 1;
to this:
else // For handling even cases
{
int idx = n / 2;
Results:
here's a working code
#include <bits/stdc++.h>
string nextSmallerPalindrome(string &s) {
// Write your code here.
string ans = s ;
if(s=="11"){
return "9";
}
int c = 0 ;
if(s.size()%2==1){
int k = s.size()/2 ;
//string ans = s ;
while(k>=0){
if(k==0 && ans[k]=='1' && ans.size()>1){
// cout<<"hola\n";
ans = ans.substr(1,ans.size()-2);
ans=ans+'9';
break;
}
if(ans[k]-'0'!=0){
int temp = ans[k]-'0';
temp-= 1 ;
ans[k] = (temp) +'0';
ans[s.size()-1-k] = (temp) +'0';
return ans;
}
else{
ans[k] = (9) +'0';
ans[s.size()-1-k] = (9) +'0';
k--;
}
}
}
else{
int k = s.size()/2 ;
while(k>=0){
if(k==0 && ans[k]=='1' && ans.size()>1){
ans = ans.substr(1,ans.size()-2);
ans=ans+'9';
break;
}
if(ans[k]-'0'!=0){
int temp = ans[k]-'0';
temp-= 1 ;
ans[k] = (temp) +'0';
ans[s.size()-1-k] = (temp) +'0';
return ans ;
}
else{
ans[k] = (9) +'0';
ans[s.size()-1-k] = (9) +'0';
if(c==0){
c++;
k-=2;
}
else k--;
}
}
}
return ans ;
}
So I have written a code to check if a long int number is Smith, but I keep getting Floating point exception: 8, no matter what size do I fix my variables in. Not quite sure what I am missing.
#include <iostream>
bool isPrime(long int k) {
if (k == 1) return false;
for (long int i = 2; i*i < k; i++)
if (k % i == 0)
return false;
return true;
}
int main(){
long int n;
std::cin >> n;
long int sumPr = 0, sumCif = 0;
while (n > 0) {
sumCif += n % 10;
n = n/10;
}
for (long int i = 0; i*i<=n/2; i++) {
if (isPrime(i)) {
while (n % i == 0){
long int p = i;
while (p > 0) {
sumPr += (p % 10);
p = p/10;
}
n = n/i;
}
}
}
if (sumPr == sumCif) std::cout << "1" ; else std::cout << "0";
return 0;
}
The limits of this loop appear to be flawed:
for (long int i = 0; i*i<=n/2; i++) {
Possibly partly due to copy and paste from isPrime(). But the larger problem is you need to modularize this code so that you can properly test each component. And reuse modules (e.g. you implement sum of digits of a number twice in your code.) Code duplication is a potential source of error.
#include <iostream>
bool isPrime(long number) {
if (number < 2) {
return false;
}
if (number % 2 == 0) {
return (number == 2);
}
for (long divisor = 3; divisor * divisor <= number; divisor += 2) {
if (number % divisor == 0) {
return false;
}
}
return true;
}
long sum_digits(long number) {
long sum = 0;
while (number > 0) {
sum += number % 10;
number /= 10;
}
return sum;
}
long sum_prime_factor_digits(long number) {
long sum = 0;
for (long divisor = 2; divisor <= number; divisor++) {
if (isPrime(divisor)) {
while (number % divisor == 0) {
sum += sum_digits(divisor);
number /= divisor;
}
}
}
return sum;
}
bool is_smith(long number) {
if (isPrime(number)) {
return false; // only composites can play this game
}
return sum_digits(number) == sum_prime_factor_digits(number);
}
int main() {
long number;
std::cin >> number;
if (is_smith(number)) {
std::cout << "1";
} else {
std::cout << "0";
}
std::cout << "\n";
return 0;
}
TESTS
> ./a.out
4
1
> ./a.out
5
0
> ./a.out
6
0
> ./a.out
22
1
> ./a.out
4937775
1
> ./a.out
15966114
1
>
Writing clean code isn't something you do after the fact, it's what you do to help you in the debugging process.
I need to know if a number has a repeating digit using recursion and return 'yes' or 'no'. I am not allowed to use loops or arrays. This is what I've done untill now with 10 global variables and it works, but I think there is a better way.
#include <iostream>
using namespace std;
int counter0 = 0;
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
int counter4 = 0;
int counter5 = 0;
int counter6 = 0;
int counter7 = 0;
int counter8 = 0;
int counter9 = 0;
bool check(int k) {
int p = k % 10;;
if (k < 10) {
return false;
} else {
if (p == 0) {
counter0++;
} else if (p == 1) {
counter1++;
} else if (p == 2) {
counter2++;
} else if (p == 3) {
counter3++;
} else if (p == 4) {
counter4++;
} else if (p == 5) {
counter5++;
} else if (p == 6) {
counter6++;
} else if (p == 7) {
counter7++;
} else if (p == 8) {
counter8++;
} else if (p == 9) {
counter9++;
}
if(counter1>1 || counter2>1 || counter3>1 || counter4>1 || counter5>1 || counter6>1 || counter7>1 || counter8>1 || counter9>1)
{
return true;
}
k=k/10;
check(k);
}
}
int main() {
//cout << "Hello, World!" << std::endl;
int n;
cin >> n;
cout << (check(n) ? "yes" : "no") << endl;
//cout << n/10;
return 0;
}
#include <iostream>
using namespace std;
bool hasRepeatingDigit(int n, int mask)
{
// base case: if we have checked all the digits and didn't find any duplicates, return false
if (n == 0)
return false;
/*
p is the place of the last digit in n (n%10).
A digit can range from 0 to 9.
The place of 0 will be 1 << 0 which is 1.
The place of 1 will be 1 << 1 which is 2.
The place of 2 will be 1 << 2 which is 4.
...
...
The place of 9 will be 1 << 9 which is 512.
*/
int p = 1 << (n % 10);
// if place of p has already been marked then it's a duplicate
if (mask&p)
return true;
// otherwise scrap the last digit (n/10), mark place p and recurse on the remaining digits
return hasRepeatingDigit(n / 10, mask|p);
}
int main()
{
int n;
cin >> n;
cout << hasRepeatingDigit(n, 0) << endl;
}
Recursion problems always have a base case and an recursive case.
The base case is simple: k<11 has no repeated digits.
For the recursive case, k has repeated digits if either:
the lower two digits of k are equal, or
k/10 has repeated digits.
So:
bool check(int k) {
if (k < 11)
return false;
int digit = k % 10;
int next = k / 10;
int digit2 = next % 10;
if (digit == digit2)
return true;
else
return check(next);
// Or in one expression:
// return (digit == digit2) || check(next);
}
first, the code is incorrect...
if you enter n=11 it says 'no' but 1 repeated twice. you can fix it by changing the if statement from if(k < 10) to if(k == 0)
you can get down to the bits level but I can't see how much is useful...
in conclusion, this is the best you can do without arrays...
BUT: if you need to find if a digit repeated twice or more in a row the other answer is perfect
The goal of this method is to find the nearest prime number less than the value passed. Any value less than 3 is undefined behavior so I just return -1. The issue lies when I pass in values 5, 20, 100, and 1, this is the output:
Passed in 5 Expected 3: 5
Passed in 20 Expected 19: 19
Passed in 100 Expected 97: 97
Passed in 1 Expected -1: -1
Here is the method:
int Hash::nearestPrime(int num){
if(num <= 3){
return -1;
}
for(int i = 2; i < num; i++){
if(num == 3){
return 3;
}
if(num % i == 0 && num != i){
num--;
i = 2;
}
}
return num;
}
You could loop and decrement until the next one is found, by having a function determine if a value is prime.
#include <iostream>
int prime (int n)
{
int i;
for (i = 2; i < n; i++)
if (n % i == 0) return 0;
return 1;
}
int run (unsigned int n)
{
while (!prime (--n));
return n;
}
int main ()
{
std::cout << run (3);
return 0;
}
Your method is right but with simple mistake.
Assign i = 1 instead of i = 2 inside the if condition. Because if you assign 2 then for i++ next checking value will start from 3. But you need to check it from 2.
int Hash::nearestPrime(int num){
if(num <= 3){
return -1;
}
for(int i = 2; i < num; i++){
if(num == 3){
return 3;
}
if(num % i == 0 && num != i){
num--;
// mistake in this line
i = 1;
}
}
return num;
}
I've been assigned to write a program that checks if a number is perfect prime or not (the sum of its digits is prime, the sum of the sum of its digits is prime...). I've stumbled upon two extreme cases that break my program:
INPUT: 20328307 OUTPUT: true (expected false)
INPUT: 587899597 OUTPUT: true (expected false)
The code:
#include <iostream>
using namespace std;
bool is_prime(int n) {
if (n == 0 or n == 1) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
for (int i = 3; i * i == n; i += 2) {
if (n % i == 0) return false;
}
return true;
}
int sum_of_digits(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
bool is_perfect_prime(int n) {
if (sum_of_digits(n) >= 10) is_perfect_prime(sum_of_digits(n)); //cas recursiu
return is_prime(n); //cas base
}
int main() {
int n;
while (cin >> n) cout << (is_perfect_prime(n) ? "true" : "false") << endl;
}
I can't see where this script fails for these two values, and why it doesn't fail for smaller numbers.
First of all your for loop is incorrect, it should be instead:
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0) return false;
}
otherwise you return true almost on every non even number. Second you ignore result of recursive call, possible solution is:
bool is_perfect_prime(int n) {
if ( n >= 10 and not is_perfect_prime(sum_of_digits(n)) )
return false;
return is_prime(n); //cas base
}
Finally I've got it to work. The problem was in the is_prime() for loop and in the recursive case of is_perfect_prime(). This is what I've come up with:
bool is_prime(int n) {
if (n == 0 or n == 1) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0) return false;
}
return true;
}
int sum_of_digits(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
bool is_perfect_prime(int n) {
if (n < 10) return is_prime(n);
if (! is_prime(n)) return false;
return is_perfect_prime(sum_of_digits(n));
}
Thanks for your answers.