How can I sum the digits of an interval of numbers and write in a file the ones that sum 13?

Hello,
I'm willing to calculate how many numbers between 90 and 20000000000 (yes, 20 billion) which the sum of its digits is exactly 13 and write this number on a file an on the onde give me the total of numbers that it found. I know there will be millions of numbers, the file will be huge, but can anyone give me a clue on this?
For example: 90 - No 91 - No 92 - No 93 - No 94 - Yes -> write to numbers.txt 95 - No and so on....
Is it possible?
Thanks!!!

3 Comments

There is a vast difference between merely counting how many such numbers there are in the given interval, and having to write each one to a file. Are you sure the latter operation is necessary? There does exist a recursive procedure that would allow you to arrive at the total count of them very quickly, but having to make a list of all such numbers would be a slow process indeed. The question is, what would you do with such a list?
Hi Roger,
We are trying to correlate some patterns between numbers which digits sum is 13, for instance. The total number of numbers between X and Y is important, but it will also be important to know which are these numbers so that we can study them.
After some research on the internet and spending a few hours reading Matlab Help (considering it's been more than 15 years that i don't "play" with Matlab), I came up with this code:
clear
c =0;
for num = [1:1000000000];
n = 0;
while (num/10^n) >= 1
n = n+1;
end
for i = 1:n
k = num - floor(num/10^i)*10^i;
num = num - k;
m(i) = k/10^(i-1);
end
if sum(m) == 13
c=c+1;
end
end
c
It seems to be returning the correct number of numbers which digit sum is 13, but i still can't find out how to store those numbers in a file.
Did you try save(), fprintf(), xlswrite(), csvwrite(), dlmwrite() or things like that?

Sign in to comment.

Answers (3)

As I stated two days ago, there is an easy recursive procedure for determining the count of n-digit numbers whose digits sum to a given value. It is more difficult to generate the corresponding numbers themselves.
For your possible interest, here is the code for finding just the counts for an n-digit number to have digits that sum to s. As you see, it is very simple.
s = 13; % Sums of digits up to s. (This code only works if s >= 9)
n = 10; % Number of digits
c = [1;zeros(s,1)];
% t = zeros(s+1,n);
for k = 1:n
c = cumsum(c);
c(11:s+1) = c(11:s+1) - c(1:s-9);
% t(:,k) = c;
end
When the for-loop exits, the vector c contains the counts of n-digit numbers for each possible sum from 0 to s. The count of numbers having sum k is found in c(k+1). When I say "n-digit number" I am including those with leading zeros, so that, for example, 000409 counts as a 6-digit number whose digits add up to 13.
If the two lines which contain the table 't' are activated, then t(k+1,m) will be the total count of numbers with m digits which sum to k. For example, the count of 6-digit numbers whose digits sum to 10, that is, among those from 000000 to 999999, would be found in t(10+1,6) = 2997.
For the particular problem you posed originally, namely the number in the interval from 90 to 20000000000, this can be derived from the above values in c with n = 10. If the eleventh digit is a 0, c(14) is the number of them from 00000000000 to 09999999999 and if the eleventh digit is set to a 1, c(13) is the number with remaining digits summing to 12, that is, among 10000000000 to 19999999999. If we subtract the five below 90, this leaves us with a total
c(14)+c(13)-4 = 495220 + 293380 - 5 = 788595
of numbers in your interval with digits adding to 13. It isn't in the millions, but it is more than three-quarters of a million. If you had all of the corresponding numbers in a long list, (as I asked you two days ago,) what would you do with them?
Yes, hint:
>> num = 94;
>> str = sprintf( '%d', num );
>> sum( sscanf( str, '%1d' ) )
ans =
13
This represent brute force. It might not be the smartest way.
You should be able to come up with a naive list. two sorted digits that sum to 13, 3 digits, and so on, and then permute those digits with a bunch of 0's as well. There'd be lots of them...

Tags

Asked:

on 6 Mar 2014

Answered:

on 9 Mar 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!