Jump to content

Can someone modify this code with a start/stop interger and output the time it took to do the work?

SVThuh

I'm wondering if someone can help me out by modifying this code so it automatically starts at say "1" and ends at say "1000000" and outputs the time it took to do the work to get there?

 

Thank you.

 

import time

def nextInt(input1):
    cont = True

    try:
        input1 = int(input1)

    except:
        return 'ERROR'


    while cont:
        
        steps = 0
        
        input2 = input1
        while input2 != 1:
            steps += 1
            if input2 % 2 == 0:
                input2 = input2 // 2
            else:
                input2 = 3 * input2 + 1
        
        value = [input1,steps, input2]
        print(value)

        time.sleep(0.0)
        if value[2] != 1:
            cont = False
        else:
            input1 += 1


nextInt(input('Input a integer: '))

 

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, SVThuh said:

I'm wondering if someone can help me out by modifying this code so it automatically starts at say "1" and ends at say "1000000" and outputs the time it took to do the work to get there?

 

Thank you.

 

import time

def nextInt(input1):
    cont = True

    try:
        input1 = int(input1)

    except:
        return 'ERROR'


    while cont:
        
        steps = 0
        
        input2 = input1
        while input2 != 1:
            steps += 1
            if input2 % 2 == 0:
                input2 = input2 // 2
            else:
                input2 = 3 * input2 + 1
        
        value = [input1,steps, input2]
        print(value)

        time.sleep(0.0)
        if value[2] != 1:
            cont = False
        else:
            input1 += 1


nextInt(input('Input a integer: '))

 

What is this code supposed to do?

Main PC [ CPU AMD Ryzen 9 7900X3D with H150i ELITE CAPPELIX  GPU Nvidia 3090 FE  MBD ASUS ROG STRIX X670E-A  RAM Corsair Dominator Platinum 64GB@5600MHz  PSU HX1000i  Case Lian Li PC-O11 Dynamic  Monitor LG UltraGear 1440p 32" Nano IPS@180Hz  Keyboard Keychron Q6 with Kailh Box Switch Jade  Mouse Logitech G Pro Superlight  Microphone Shure SM7B with Cloudlifter & GoXLR ]

 

Server [ CPU AMD Ryzen 5 5600G  GPU Intel ARC A380  RAM Corsair VEGEANCE LPX 64GB  Storage 16TB EXOS ]

 

Phone [ Google Pixel 8 Pro, 256GB, Snow ]

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, SVThuh said:

It is just a code that automates the Collatz conjecture.

 

https://en.wikipedia.org/wiki/Collatz_conjecture

 

Okay, in that case, try something like this, note that I removed the print statement, you can re-add it but it will increase your run time exponentially.

import time

def nextInt(input1, max_input):
    start_time = time.time()  # Record the start time
    cont = True

    try:
        input1 = int(input1)
    except:
        return 'ERROR'

    while cont and input1 <= max_input:
        steps = 0
        input2 = input1

        while input2 != 1:
            steps += 1
            if input2 % 2 == 0:
                input2 = input2 // 2
            else:
                input2 = 3 * input2 + 1
        
        value = [input1, steps, input2]
        
        if input1 != max_input:
            input1 += 1
        else:
            cont = False

    end_time = time.time()  # Record the end time
    total_time = end_time - start_time
    print(f"Total running time: {total_time} seconds")

max_value = int(input('Enter the maximum input value: '))
nextInt(1, max_value)

Output:

Enter the maximum input value: 1000000
Total running time: 7.543521404266357 seconds

 

I did have to make some changes to your original function, I don't think it was working as intended when I ran it.

Main PC [ CPU AMD Ryzen 9 7900X3D with H150i ELITE CAPPELIX  GPU Nvidia 3090 FE  MBD ASUS ROG STRIX X670E-A  RAM Corsair Dominator Platinum 64GB@5600MHz  PSU HX1000i  Case Lian Li PC-O11 Dynamic  Monitor LG UltraGear 1440p 32" Nano IPS@180Hz  Keyboard Keychron Q6 with Kailh Box Switch Jade  Mouse Logitech G Pro Superlight  Microphone Shure SM7B with Cloudlifter & GoXLR ]

 

Server [ CPU AMD Ryzen 5 5600G  GPU Intel ARC A380  RAM Corsair VEGEANCE LPX 64GB  Storage 16TB EXOS ]

 

Phone [ Google Pixel 8 Pro, 256GB, Snow ]

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, SVThuh said:

I'm wondering if someone can help me out by modifying this code so it automatically starts at say "1" and ends at say "1000000" and outputs the time it took to do the work to get there?

When you say this do you mean like testing the numbers 1 - 1,,000,000?

 

If so it's as simple as the following

import time

# Your code

start_time = time.time()
for number_to_test in range(1,1000001):
	next_int(number_to_test)
    
delta_time = time.time() - start_time

 

Not entirely important as you won't hit any number in python that runs into this problem...but if the conjecture if false that means there will be a counter example...in that case your program would technically have an infinite loop in it if the counter example input was put in

3735928559 - Beware of the dead beef

Link to comment
Share on other sites

Link to post
Share on other sites

On 10/8/2023 at 2:55 PM, Zalosath said:

Okay, in that case, try something like this, note that I removed the print statement, you can re-add it but it will increase your run time exponentially.

import time

def nextInt(input1, max_input):
    start_time = time.time()  # Record the start time
    cont = True

    try:
        input1 = int(input1)
    except:
        return 'ERROR'

    while cont and input1 <= max_input:
        steps = 0
        input2 = input1

        while input2 != 1:
            steps += 1
            if input2 % 2 == 0:
                input2 = input2 // 2
            else:
                input2 = 3 * input2 + 1
        
        value = [input1, steps, input2]
        
        if input1 != max_input:
            input1 += 1
        else:
            cont = False

    end_time = time.time()  # Record the end time
    total_time = end_time - start_time
    print(f"Total running time: {total_time} seconds")

max_value = int(input('Enter the maximum input value: '))
nextInt(1, max_value)

Output:

Enter the maximum input value: 1000000
Total running time: 7.543521404266357 seconds

 

I did have to make some changes to your original function, I don't think it was working as intended when I ran it.

I appreciate all the work, but it doesn't seem to be working correctly on my end.  I am able to enter the maximum input value and as far as I can see, it does the math, but does not print the resultant running time.  The window just closes.  Any help would be greatly appreciated. 

 

I attached a screen record of what I am witnessing. 

 

Thank you

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

37 minutes ago, SVThuh said:

I appreciate all the work, but it doesn't seem to be working correctly on my end.  I am able to enter the maximum input value and as far as I can see, it does the math, but does not print the resultant running time.  The window just closes.  Any help would be greatly appreciated. 

 

I attached a screen record of what I am witnessing. 

 

Thank you

 

 

Timeline 1.mov

Just add an input() to the end of the function:

import time

def nextInt(input1, max_input):
    start_time = time.time()  # Record the start time
    cont = True

    try:
        input1 = int(input1)
    except:
        return 'ERROR'

    while cont and input1 <= max_input:
        steps = 0
        input2 = input1

        while input2 != 1:
            steps += 1
            if input2 % 2 == 0:
                input2 = input2 // 2
            else:
                input2 = 3 * input2 + 1
        
        value = [input1, steps, input2]
        
        if input1 != max_input:
            input1 += 1
        else:
            cont = False

    end_time = time.time()  # Record the end time
    total_time = end_time - start_time
    print(f"Total running time: {total_time} seconds")
    input("Press any key to exit")

max_value = int(input('Enter the maximum input value: '))
nextInt(1, max_value)

 

Main PC [ CPU AMD Ryzen 9 7900X3D with H150i ELITE CAPPELIX  GPU Nvidia 3090 FE  MBD ASUS ROG STRIX X670E-A  RAM Corsair Dominator Platinum 64GB@5600MHz  PSU HX1000i  Case Lian Li PC-O11 Dynamic  Monitor LG UltraGear 1440p 32" Nano IPS@180Hz  Keyboard Keychron Q6 with Kailh Box Switch Jade  Mouse Logitech G Pro Superlight  Microphone Shure SM7B with Cloudlifter & GoXLR ]

 

Server [ CPU AMD Ryzen 5 5600G  GPU Intel ARC A380  RAM Corsair VEGEANCE LPX 64GB  Storage 16TB EXOS ]

 

Phone [ Google Pixel 8 Pro, 256GB, Snow ]

Link to comment
Share on other sites

Link to post
Share on other sites

6 hours ago, Zalosath said:

Just add an input() to the end of the function:

import time

def nextInt(input1, max_input):
    start_time = time.time()  # Record the start time
    cont = True

    try:
        input1 = int(input1)
    except:
        return 'ERROR'

    while cont and input1 <= max_input:
        steps = 0
        input2 = input1

        while input2 != 1:
            steps += 1
            if input2 % 2 == 0:
                input2 = input2 // 2
            else:
                input2 = 3 * input2 + 1
        
        value = [input1, steps, input2]
        
        if input1 != max_input:
            input1 += 1
        else:
            cont = False

    end_time = time.time()  # Record the end time
    total_time = end_time - start_time
    print(f"Total running time: {total_time} seconds")
    input("Press any key to exit")

max_value = int(input('Enter the maximum input value: '))
nextInt(1, max_value)

 

This worked great. Thank you!

Link to comment
Share on other sites

Link to post
Share on other sites

On 10/8/2023 at 4:55 PM, Zalosath said:
Total running time: 7.543521404266357 seconds

You might have a couple problems in the algorythm. I can't pin point that language but there is no way collatz conjecture takes that much time for 1,000,000. It should be more like sub 1 second

Link to comment
Share on other sites

Link to post
Share on other sites

26 minutes ago, Franck said:

You might have a couple problems in the algorythm. I can't pin point that language but there is no way collatz conjecture takes that much time for 1,000,000. It should be more like sub 1 second

I copied OPs code. The problem with it is the while in a while loop. For whatever reason, OP is running collatz conjecture over and over again, the main part of their function:

while input2 != 1:
	steps += 1
    	if input2 % 2 == 0:
        	input2 = input2 // 2
        else:
            input2 = 3 * input2 + 1

Is the actual collatz conjecture calculation (bar the input1 != 1 part, which should really be > 1) (and in-fact is should more likely be / instead of //), the rest of their function is just running it multiple times. Perhaps they have a specific goal in mind with this? Who knows.

 

A more optimal function that I stole from StackOverflow is the following, with the timer included:

import time

def collatz_sequence(x):
    seq = [x]
    if x < 1:
       return []
    while x > 1:
       if x % 2 == 0:
         x = x / 2
       else:
         x = 3 * x + 1 
       seq.append(x)    # Added line
    return seq

start = time.time()
print(collatz_sequence(1000000))
end = time.time()
total = end - start
print(total)

Without printing the output of the sequence, 0.0 is printed, with print, it's 0.0105...

Main PC [ CPU AMD Ryzen 9 7900X3D with H150i ELITE CAPPELIX  GPU Nvidia 3090 FE  MBD ASUS ROG STRIX X670E-A  RAM Corsair Dominator Platinum 64GB@5600MHz  PSU HX1000i  Case Lian Li PC-O11 Dynamic  Monitor LG UltraGear 1440p 32" Nano IPS@180Hz  Keyboard Keychron Q6 with Kailh Box Switch Jade  Mouse Logitech G Pro Superlight  Microphone Shure SM7B with Cloudlifter & GoXLR ]

 

Server [ CPU AMD Ryzen 5 5600G  GPU Intel ARC A380  RAM Corsair VEGEANCE LPX 64GB  Storage 16TB EXOS ]

 

Phone [ Google Pixel 8 Pro, 256GB, Snow ]

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, Franck said:

You might have a couple problems in the algorythm. I can't pin point that language but there is no way collatz conjecture takes that much time for 1,000,000. It should be more like sub 1 second

You are correct.. To run the math on "1,000,000" the time to do the work took 0.0010013580322265625 seconds.  I'm more interested in how long it takes to get to "1,000,000", running the math, starting at "1" then "2", etc. up to 1 mil.

Link to comment
Share on other sites

Link to post
Share on other sites

When timing code in Python you should generally use the timeit module from the standard library rather than the time module. There's a couple of issues with repeatability of timing snippets that result from using time alone. If being accurate isn't really important then it doesn't matter, but it's good to know

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×