timemath
Differences
This shows you the differences between two versions of the page.
| timemath [2025/04/06 05:57] – created appledog | timemath [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 == ' | ||
| + | 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 | ||
| + | |||
| + | </ | ||
| + | |||
| + | === Testing | ||
| + | < | ||
| + | def test(): | ||
| + | # set up the array | ||
| + | a = [] | ||
| + | for x in range(1_000_000): | ||
| + | a.append(random.randint(1, | ||
| + | |||
| + | 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" | ||
| + | |||
| + | </ | ||
timemath.1743919047.txt.gz · Last modified: 2025/04/06 05:57 by appledog
