Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Erratic Datetime result
#1
To practise coding, I've been looking at Problem 14 of Project Euler, the longest Collatz sequence for numbers up to a million.
My code takes about 35 seconds to process a million numbers, and to get an estimate for the time to process 10 million numbers, I extracted the times for the powers of 10 (to practise coding, and a bit curious).

# Problem 14 Euler Project - Longest Collatz Sequence

from datetime import datetime


def iterate(n):
    numberIterations = 0
    while n > 1:
        if n % 2 == 0:
            n = n / 2
        else:
            n = 3 * n + 1
        numberIterations += 1
    return numberIterations


t1 = datetime.now()
term = []
timings = []
timeinterval = []
timings.append(t1)
no = 1
exponent = 1
maxsteps = 0
maxlimit = 1000 
for x in range(1, maxlimit+1):
    if x == 10**exponent:
        term.append(x)
        t2 = datetime.now() #This is the troublesome line 
        timings.append(t2)
        timeinterval.append(t2-t1)
        exponent += 1
        if iterate(x) > maxsteps:
            maxsteps = iterate(x)
            no = x
    else:
        if iterate(x) > maxsteps:
            maxsteps = iterate(x)
            no = x
print(maxsteps, no)
print(term)
print(timings)
print(timeinterval)
The line t2 = datetime.now() works erratically. This is the correct output.

Output:
178 871 [10, 100, 1000] [datetime.datetime(2018, 12, 27, 12, 52, 48, 557358), datetime.datetime(2018, 12, 27, 12, 52, 48, 557829), datetime.datetime(2018, 12, 27, 12, 52, 48, 558330), datetime.datetime(2018, 12, 27, 12, 52, 48, 575374)] [datetime.timedelta(microseconds=471), datetime.timedelta(microseconds=972), datetime.timedelta(microseconds=18016)]
This is the same code (hitting the Rerun button) with nothing changed:
Output:
178 871 [10, 100, 1000] [datetime.datetime(2018, 12, 27, 14, 26, 11, 347978), datetime.datetime(2018, 12, 27, 14, 26, 11, 347978), datetime.datetime(2018, 12, 27, 14, 26, 11, 349454), datetime.datetime(2018, 12, 27, 14, 26, 11, 367000)] [datetime.timedelta(0), datetime.timedelta(microseconds=1476), datetime.timedelta(microseconds=19022)]
The first instance of t2 in this second case is the same value as t1. There is no (to me) obvious pattern.

If a breakpoint is placed at t2 = datetime.now(), t2 is evaluated correctly at about 500 millionth of a second.

So my question is: "what am I doing wrong"?

I'm also wondering how to get rid of the datetime.datetime appearing each time in the output.

This is my first posting.

Thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Confusion with datetime 'dst' function result jsmith1703 4 3,286 Nov-16-2019, 02:55 AM
Last Post: DeaD_EyE
  TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str' findbikash 2 9,509 Sep-18-2019, 08:32 AM
Last Post: buran
  SQLAlchemy DateTime result 0's AMarotta97 0 2,249 Oct-01-2018, 04:35 AM
Last Post: AMarotta97
  datetime unexpected result PickyBiker 10 8,957 Dec-27-2016, 10:47 PM
Last Post: PickyBiker

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020