timemath
Table of Contents
Time and Math
Time
isEven
def main(): a = 5 if iseven(a): print(f"{a} is even.") else: print(f"{a} is odd.") def iseven(a): return True if __name__ == "__main__": main()
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
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")
timemath.txt · Last modified: 2025/04/06 06:34 by appledog