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

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...

Related

How do I find the last digit of a very big number that is calculated by a formula?

I have a fibonacci problem where I want to calculate nth fibonacci and want its last digit (digit I get when I'll do it % 10). n would be given and can be up to 10^18.
unsigned long long int nthfib(long long int n) {
double phi = (1 + sqrt(5)) / 2;
return round(pow(phi, n-1) / sqrt(5));
}
The above code, for large n, e.g. 1024, gives such a big number that I can't store it in a variable and find its % 10.
As time is an issue, I want the solution in O(1).
Fibonacci numbers follow a pattern where the last digit repeats every 60 numbers. See http://mathworld.wolfram.com/FibonacciNumber.html
The sequence of final digits in Fibonacci numbers repeats in cycles of 60. The last two digits repeat in 300, the last three in 1500, the last four in 15000, etc. The number of Fibonacci numbers between n and 2n is either 1 or 2 (Wells 1986, p. 65).
Wells, D. The Penguin Dictionary of Curious and Interesting Numbers. Middlesex, England: Penguin Books, pp. 61-67, 1986.
To get the last digit of the nth number in a constant time, you can calculate the last digits of the first 60 numbers into an array and then return the n % 60th digit in that array.
If you want just the last digit you don't need to store the entire number for it.
a = 0; // return a if n==1
b = 1; // return b if n==2
for(i=2;i<n;i++){
c = (a+b)%10;
a=b;
b=c;
}
return b;
Instead of storing the entire number you are just storing their last digits.

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

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

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.

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