Basically, I need to generate a list of all seven-digit numbers (for example, 1461979) from 0000000 to 9999999. I know there are 10^7 of them (ten million), but I can't think of an efficient function to output them. I'm sure there's a generic function out there that can do it - preferably in PHP, but I can port it if need be.
Asked
Active
Viewed 367 times
-1
-
Just a comment - instead of using printf() write all results to file. You'll get 70 Mb of data, not much for a modern system. – Eugene Mayevski 'Callback Jan 13 '11 at 20:56
-
@Eugene: 80 or 90 MB actually; you forgot the EOL. – Ignacio Vazquez-Abrams Jan 13 '11 at 20:58
-
@Ignacio depends. If the data is strictly formatted to 7 chars per value, there's no need for EOL. And if the string representation of numbers doesn't contain leading zeros, then the result will be less than 70 Mb (too lazy to calculate precisely now :) – Eugene Mayevski 'Callback Jan 13 '11 at 21:00
-
1Can you generate a list of all two digit numbers? If so, then it is trivial to generate all seven digit numbers. If not, then you have a more basic problem. – President James K. Polk Jan 14 '11 at 00:06
-
I'm creating an API to access a service where one of the parameters is a seven-digit ID. – MarathonStudios Jan 14 '11 at 03:33
2 Answers
6
Loop from 0 to 9999999 and use one of the dozens of methods for zero-filling a number, such as printf("%07d", $val)
.

Ignacio Vazquez-Abrams
- 776,304
- 153
- 1,341
- 1,358
4
for ($i=0; $i<=9999999; $i++) {
echo sprintf("%07d", $i) . '<br / >';
}
This might break your browser though ;)

Alec
- 9,000
- 9
- 39
- 43