how to find integers over a range which contains different digits [duplicate] - c++

This question already exists:
Closed 10 years ago.
Possible Duplicate:
c++ program to find total numbers of integers containing different digits
Suppose i have a unsigned integer, call it low and one another call it high such that high>low. The problem is to find integers which contains distinct digits over this range. For example, suppose low is 1 and high is 10 then the answer is 10, because all the numbers in this range contains distinct digits. If suppose low is 1 and high is 12, then the answer is 10, because 11 contains same digits.example 123,234,4567 is valid number but 121,2342,4546 is invalid number.I am not looking for a bruteforce algo., if anyone has a better solution then a usual bruteforce approach, please tell..

I would derive an algorithm to determine the number of such digits from 0-n, then you could simply compute (# of valid numbers 0-high) - (# of valid numbers 0-low). To get the valid numbers 0-n, look at the number of digits in your number: if n has 5 digits for example, every valid 1, 2, 3, and 4 digit number is in your result set. So for say a 4 digit number, you compute all the possible combinations of digits in that 4 digit number: 1234, 1235, 1236...5678, 5789, and 6789. Then count the number of permutations (1234 can also be 1243, 1324, 1342...etc), and multiply (# of permutations) x (# of distinct sequences you derived in the previous step). Then you have your answer for all 4 digit numbers. Do the same for each other set, and come up with something more specific for your last set; if high is 5500, you need valid numbers between 5000-5100. You can apply a similar algorithm, but instead of using all digits 0-9, you instead use 9 distinct digits, omitting the '5'. Note that all numbers can have a 0 in them as well, but not at the beginning, so the algorithm would need to account for that as well.

Simply convert your number to a string and then run over it while checking if the given char has already occured in your string. e.g. :
#include <string>
int main()
{
std::string s = std::to_string(12345);
bool occuredCheck[10] = {0}; //automatically fills with zeros. 10 values for the 10 numbers
bool isValidNumber = true;
for(int i=s.length()-1; i>=0; ++i)
if(occuredCheck[s[i] - '0'] ^ true == 0) isValidNumber = false;
}
The if-line set's the array-entry to zero, when a diggit occured twice, see XOR.
And isValidNumber lets you know if it actually is a valid number for you.
BTW: This example needs C++11 for std::to_string.
Using this algorithm you may detect the first invalid number and then set your range using it.

Related

Required: large number (max 1000 digits) stored in string modulo 11

I have a question, which is to find the modulo 11 of a large number. The number is stored in a string whose maximum length is 1000. I want to code it in c++. How should i go about it?
I tried doing it with long long int, but its impossible that it can handle the corner case value.
A number written in decimal positional system as a_na_{n-1}...a_0 is the number
a_n*10^n+a_{n-1}*10^{n-1}+...+a_0
Note first that this number and the number
a_0-a_{1}+a_{2}+...+(-1)^{n}a_n
which is the sum of its digits with alternating signs have the same remainder after division by 11. You can check that by subtracting both numbers and noting that the result is a multiple of 11.
Based on this, if you are given a string consisting of the decimal representation of a number, then you can compute the remainder modulo 11 like this:
int remainder11(const std::string& s) {
int result{0};
bool even{true};
for (int i = s.length() - 1; i > -1; --i) {
result += (even ? 1 : -1) * ((int)(s[i] - '0'));
even = !even;
}
return ((result % 11) + 11) % 11;
}
Ok, here is the magic (math) trick.
First imagine you have a decimal number that consists only of 1s.
Say 111111, for example. It is obvious that 111111 % 11 is 0. (Since you can always write it as the sum of a series of 11*10^n). This can be generalized to all integers consists purely of even numbers of ones. (e.g. 11, 1111, 11111111). For those with odd number of ones, just subtract one from it and you will get a 10 times some number that consists of odd numbers of one (e.g 111=1+11*10), so their modulo to 11 would be 1.
A decimal number can be always written as the form of
where a0 is the least significant digit and an is the most significant digit. Note that 10^n can be written as 10^n - 1 + 1, and 10^n - 1 is a number consists of n nines. If n is even, then you will get 9 times some even number of ones, and its modulo to 11 is always 0. If n is odd, then we get 9 times some odd number of ones, and its modulo to 11 is always 9. And don't forget we've still got a +1 after 10^n - 1 + 1 so we need to add a to the result.
We are very close to our results now: we just have to add things up and do a final modulo to 11. The pseudo-code would be like:
Initialize sum to 0.
Initialize index to 0.
For every digit d from the least to most significant:
If the index is even, sum += d
Otherwise, sum += 10 * d
++index
sum %= 11
Return sum % 11

Modulus of same number returns different results

int a=032302;
cout<<a%10<<endl; // output 6
int b=32302;
cout<<b%10<<endl; // output 2
I was trying to get the unit's place of a number but while coding i found a weird thing, the first and the second no are technically same, however they both output different results.
The first one returns 6 while the second one 2 , am i missing something here?
Starting a numeral with 0 (zero) in c/c++ means it is an octal (base 8) number. Thus 032302 is 13506 in decimal notation. Hence, the last digit is 6 and that is what you get from your modulus operation.
Considering the fact that
int a = 032302;
and
int b = 13506;
are holding the same integer value since variable a is init as octal literal
then is correct that
a%10 returns 6 same as b%10 returns 6

How to generate a 4 distinct digit random number in c++? [duplicate]

This question already has answers here:
Four digit random number without digit repetition
(5 answers)
Closed 6 years ago.
#include <bits/stdc++.h>
using namespace std;
int main()
{
srand( time(NULL) );
int number = (rand() % 9000)+1000;
cout<<number<<endl;
}
This code helps in generating a 4 digit number but not a distinct one i.e it generates numbers like 4545 , 1561 ,9999 etc.. whereas I want numbers like 1234 ,2395 etc...
You could roll numbers until you get one with distinct digits, but that would be extremely inefficient.
You can either:
generate the first digit, and then
generate each next digit until it is different from the previous ones
put all digits together to form the number
or (more efficient):
roll a number between 0-9 for the first digit d1
roll a number between 0-8 for the second digit d2 , if d2>=d1 then set d2 += 1
roll a number between 0-7 for the third digit, etc...

How to calculate nth n-digit palindrome efficiently?

I think the question is simple enough to understand.For more clarity I'm giving example :
In the list of 2 digit palindromes, the 7th palindrome is 77 (1st being 11, 2nd being 22 and so on).
Obviously a brute force solution exists but it's not efficient.
Can anyone suggest me some better solution to solve the problem ?
First, we can simplify the problem because we only need to look at the first half of the digits (rounding up if there are an odd number of digits). I will call the first set of digits significant digits and the rest non-significant digits.
This is because the non-significant digits must match the significant digits (in reverse). It is not possible to have another palindrome number with the same leading significant digits and different non-significant digits. The significant digits determine the entire palindrome number.
Now, we just need to come up with an algorithm to generate the nth valid significant digits. This would be easier if we allowed for leading zeros, so we'll come up with the algorithm that allows for leading zeros, then tweak the algorithm.
The first few palindromes (significant digits) would be:
1: 0000
2: 0001
3: 0002
...
100: 0099
So we can find the significant digits of the nth number by finding the decimal representation of (n-1).
To tweak the algorithm to work when not allowing leading zeros, we would start with a one as the leading digit:
1: 1000
2: 1001
3: 1002
...
100: 1099
This boils down to finding the decimal representation of (n-1) + 1000 = n + 999 and expanding into a full palindrome:
Example: Find the 113th palindrome of length 9.
Determine number of digits to look at: Round up(9 / 2) = 5 --> only look at first 5 digits.
Find number to add to get rid of leading zeros: 10^(5-1) = 10000
Use formula: (113 - 1) + 10000 = 10112
Expanded into palindrome: 101121101
On a side note, this algorithm could also be generalized to finding the nth palindrome of any ordered set of symbols (or alphabet).
Generalized algorithm:
Given: finding palindrome number n , palindrome has m symbols as digits , there are p symbols (10 symbols in decimal)
Let q = ceiling(m / 2)
Let offset = p ^ (q - 1)
Let number = (n - 1) + offset
Let answer be number expanded as a palindrome
The first few 7-digit palindrome are:
1000001
1001001
1002001
1003001
...
1009001
1010101
1011101
...
I think it's very easy to see from the pattern of what is the nth m-digit palindrome...
When the number of digits is even, just take the nth number with half as many digits starting from 100..0, where the length is half the number of digits. The palindrome is just this number followed by its mirror.
For an odd number of digits, just take the ceiling of half that number, and count from 100...0 the same way. Then the palindrome is this number followed by its mirror with the first digit removed.
The 65th 10 digit number:
65 + 9999 = 10064
1006446001
The 10298th 13 digit number:
10298 + 999999 = 1010297
1010297920101
For two digit palindromes the difference between two consecutive palindromes is 11.
something like:
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
using namespace std;
void reverseString(char *dest,char *src){
char *iter=src;
while (*iter) iter++;
while (iter!=src){
iter--;
*dest=*iter;
dest++;
}
*dest=0;
}
char *pal(int n,int len){
char *tmp=new char[len/2+2];
char *out=new char[len+1];
sprintf(out,"%i",n+int(pow(10.0,(len+1)/2-1))-1);
reverseString(tmp,out); //copy reversed out into tmp
strcat(out,tmp+len%2);
delete []tmp;
return out;
}
int main(){
cout<<pal(4,7)<<endl;
}

Need help with C++ Loops Exercise

From Cay Horstmann's "C++ For Everyone"
Chapter 4: Loops
Write a program that adds up the sum of all odd digits of n. (For example, if n is 32677, the sum would be 3 + 7 + 7 = 17)
I don't know how to make the computer "see" the numbers like separate them
n % 10 gets the value of the one's digit. You can figure it out from there right?
Here's a hint. C++ has the modulus operator %. It will produce the remainder when two numbers are divided together. So if I wanted to know the last digit in a number which was greater than 10 I would modulus 10 and get the result
int lastDigit = number % 10;
The last digit of a base-10 integer i is equal to i % 10. (For reference, % is the modulus operator; it basically returns the remainder from dividing the left number by the right.)
So, now you have the last digit. Once you do, add it to a running total you're keeping, divide i by 10 (effectively shifting the digits down by one place), or in your case 100 (two places), and start back at the beginning. Repeat until i == 0.
People here rather not provide you with the answer to your exercise, but to provide you with hints so that you can find the answer on your own and more importantly understand it.
To start, the following arithmetic operations will help you:
loop:
right_most_digit = n % 10
n = n / 10
end_loop