When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
21 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
Not sure that I understand... You want to count the number of digits that are in a random long long value? Why not just treat zero as a special case and return 1 if the value is zero. Otherwise, you repeatedly divide the value by 10 until it is zero and count the number of divisions you had to do. Is there something I don't understand here?
im not helping cause the title is terrible. the title is for the topic. not begging.
I don't understand what you want to do
All the digits that are in all the numbers from 1 to N.
Counting the number of digits from 1 to a random integer up to LLONG_MAX
@rapid snow
Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity
it is actually just a variation on what I described already. init an array of counts for each digit to zero. if it is zero just increment the zero counter and you are done. Otherwise, while the number is greater than zero pull off the bottom digit with int digit = number % 10. Use digit as an index into the array of counters and increment it. Divide the number by 10 and continue. Don't give up so easily...
#include <iostream>
#include <cmath> // For abs()
void countDigits(long long number) {
int digitCount[10] = {0};
number = std::abs(number);
if (number == 0) {
digitCount[0]++;
}
while (number > 0) {
int digit = number % 10; // Get the last digit
digitCount[digit]++; // Increment the count for that digit
number /= 10; // Remove the last digit
}
for (int i = 0; i < 10; ++i) {
std::cout << "Digit " << i << ": " << digitCount[i] << " times\n";
}
}
int main() {
long long number;
std::cout << "Enter a number: ";
std::cin >> number;
countDigits(number);
return 0;
}
Well, it's much more complicated than that. What I need to do is to get the accumulated counts of digits from 1 all the way up to LLONG_MAX.
I mean, I've been trying to find a way to count the zeroes properly... Here's my code:
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
inline long long int powers(const int& n) {
return n * (long long int)pow(10, n - 1);
}
inline long long int zeroes(const int& n) {
long long int val = 0;
for (int i = 1; i <= n; i++) {
val += (long long int)pow(10, n - 1);
}
return val;
}
void count_digits(vector<long long int>& ans, const int& n) {
int exp = (int)log10(n);
long long int first_digit = n / (long long int)pow(10, exp);
if (n < 10) {
for (int i = 1; i <= n; i++) {
ans[i]++;
}
return;
}
else if (n == first_digit * (long long int)pow(10, exp)) {
count_digits(ans, n - 1);
ans[first_digit]++;
ans[0] += exp;
return;
}
else if (n + 1 == (long long int)pow(10, exp + 1)) {
for (int i = 1; i <= first_digit; i++) {
for (int j = 0; j <= 9; j++) {
ans[j] += powers(exp);
}
}
for (int i = 1; i < first_digit; i++) {
ans[i] += (long long int)pow(10, exp);
}
ans[first_digit] += n % (long long int)pow(10, exp) + 1;
long long int r = n % (long long int)pow(10, exp);
count_digits(ans, r);
}
else {
long long int q = first_digit * (long long int)pow(10, exp);
long long int r = n % (long long int)pow(10, exp);
count_digits(ans, q);
ans[first_digit] += r;
count_digits(ans, r);
return;
}
}
int main() {
vector<long long int> ans(10, 0);
long long int n;
cin >> n;
count_digits(ans, n);
for (long long int& x : ans) {
cout << x << ' ';
}
return 0;
}
"
Okay, that also has an issue since pow's default data type is double, where it cannot store the value up to LLONG_MAX...
Counting the accumulated number of digits from 1 to a random integer up to LLONG_MAX
are you allowed to use arbitrary precision libraries in your solution?
Any ad hoc solution is allowed.
Is this close to what you expect to find?
$ ./a.out
Digit 0: 17688124484043666577
Digit 1: 17688124447188890761
Digit 2: 17604867557753218185
Digit 3: 17577467517753218185
Digit 4: 17577023437188666569
Digit 5: 17577023437182666377
Digit 6: 17577023437032665569
Digit 7: 17577015473032637185
Digit 8: 17577013436132555476
Digit 9: 16800385472932555468
There are a number of arbitrary precision libraries available. What I used was GMP (GNU Multiple Precision Arithmetic Library). https://gmplib.org/
Yeah.