What time it would take?

what time it takes to printing all 6 digit numbers in python?

Sorry, I searched it in google but didn’t find the expected result. Please help. It is very important. My code for printing this:

for i in range(100000,1000000):
    print(i)

It is a simple code. I just have to know what time it takes to complete the printing. Did anyone try it before? Did anyone try to print all 6 digit number? if yes, please help. I am practically doing it but in my old and slow computer it stuck.

O(9 * 10^5)

In python it might be a bit slower, but I am talking in a general view. In Python I guess it should be O(3 * 10^6). But I have no clue as I don’t code in python.

1 Like

Suppose, 900000 sec
= 15000 min
= 250 h
= 10 days 4 hour!
O shit! It is so late :pensive:. Is there any way to print all number in 1 day? :neutral_face:

The computer does ~10^8 operations a second. So it will get processed within a second, but to output those many numbers, it will take time in the terminal.

1 Like
HP@HP-PC ~/temp
$ uname -a
CYGWIN_NT-6.1 HP-PC 3.1.4(0.340/5/3) 2020-02-19 08:45 i686 Cygwin

HP@HP-PC ~/temp
$ cat prog.py
for i in range(100000, 1000000):
    print(i)

HP@HP-PC ~/temp
$ time python prog.py > log.txt

real    0m5.690s
user    0m0.000s
sys     0m0.015s

Is this what you’re looking for? :slightly_smiling_face:

3 Likes

Thanks! Where did you calculate it? :thinking:

1 Like

What specification of computer, if you don’t mind me asking? I just tried on my Raspberry Pi 3, and it took 15 seconds.

Edit:

4 seconds, if I log to a file like @raisinten did.

1 Like

@ssjgz In my computer it takes 56.5 sec.

But when I tried to solve this, I wait 30 minutes but it didn’t give me any output. At last, it gave me a Memoryerror.

s3 = []
s = []
s5 = []
for i in range(100000,1000000):
    a = str(i)
    li = list(a)
    s1 = int(li[0])+int(li[1])+int(li[2])+int(li[3])+int(li[4])+int(li[5])
    s2 = int(li[0])*int(li[1])*int(li[2])*int(li[3])*int(li[4])*int(li[5])
    k = 1
    for _ in range(100000):
        a = s1%k
        b = s2%k
        if a == 0:
            s.append(a)
        if b == 0:
            s5.append(b)
        k = k + 1
    if len(s) == 2 and len(s5) == 2:
        s3.append(i)
print(s3)

P S: I was trying to solve this problem- How many numbers are there which contain six digits and the sum and product of the digits are both prime numbers?

Why are you even iterating through all possible numbers. Since you want the product of six digits of the number to be prime, then it is only possible if there are 5 1’s in the number and a 1 digit prime number as the sixth number. i.e possible numbers are.
1 1 1 1 1 2 : sum = 7
1 1 1 1 1 3 : sum = 8
1 1 1 1 1 5 : sum = 10
1 1 1 1 1 7 :sum = 12
hence six numbers with sum and product as prime numbers are 1 1 1 1 1 2
and there are a total of 6!/(5!*1!) i.e 6 permutations hence shouldn’t the ans just be 6 , please correct me if I am wrong anywhere

2 Likes

@cubefreak777 Thanks. I didn’t think this way. Your help save my lot of time.

1 Like

I’m using Cygwin (which I highly recommend for Windows users) which provides me with the time command to calculate the time taken by a command.

I stole a look at the image you just deleted :stuck_out_tongue_winking_eye: and came to know that you’re using the cmd of a Windows 7. In case you don’t feel like downloading Cygwin (which is totally fine :slightly_smiling_face:) you may use the equivalent for timing a command, i.e., the Measure-Command command available in the PowerShell which you already have on your system.

I just used it on your script with a tiny modification because it was taking up too much time.

PS C:\cygwin\home\HP\temp> cat .\prog.py
for i in range(100000, 200000):
    print(i)
PS C:\cygwin\home\HP\temp> Measure-Command {python .\prog.py > .\log.txt}


Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 24
Milliseconds      : 950
Ticks             : 249500344
TotalDays         : 0.000288773546296296
TotalHours        : 0.00693056511111111
TotalMinutes      : 0.415833906666667
TotalSeconds      : 24.9500344
TotalMilliseconds : 24950.0344

Hope this helps. :slightly_smiling_face:

3 Likes

@raisinten Thank you very much! That’s a great help!

1 Like

You’re welcome. :slightly_smiling_face: