User Tools

Site Tools


timemath

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

timemath [2025/04/06 05:57] – created appledogtimemath [2025/04/06 06:34] (current) appledog
Line 21: Line 21:
  
 We'll start by fixing this. Then, we'll make a function that tests if a number is prime. Then we will try to generate the first million prime numbers. But, quickly! We'll start by fixing this. Then, we'll make a function that tests if a number is prime. Then we will try to generate the first million prime numbers. But, quickly!
 +
 +=== other isEven
 +def iseven_bad(a):
 +    b = float(a) / 2
 +    c = str(b)
 +    d = c[-1]
 +
 +    if d == '0':
 +        return True
 +
 +    return False
 +
 +def iseven_good(a):
 +    b = a % 2
 +    if b == 0:
 +        return True
 +
 +    return False
 +
 +def iseven_bit(a):
 +    if a & 1:
 +        return False
 +    else:
 +        return True
 +
 +def iseven_count(a):
 +    b = a
 +    while b > 0:
 +        b = b - 2
 +        if b == 1:
 +            return False
 +
 +    return True
 +
 +</Code>
 +
 +=== Testing
 +<Code:Python>
 +def test():
 +    # set up the array
 +    a = []
 +    for x in range(1_000_000):
 +        a.append(random.randint(1,1000))
 +
 +    start = time.perf_counter_ns()
 +
 +    c = 0
 +    for x in range(1_000_000):
 +        r = iseven_good(a[c])
 +        c = c + 1
 +
 +    ms = (time.perf_counter_ns() - start) / 1_000_000
 +    print(f"run time: {ms} ms")
 +
 +</Code>
timemath.txt · Last modified: 2025/04/06 06:34 by appledog

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki