File size: 4,164 Bytes
32ecc14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"""Benchmark programs offered as one-click presets.

Each is chosen to be a fair test of translation rather than a trick: pure
computation, deterministic output, no I/O or third-party imports, and a runtime
long enough in Python that the speedup is unambiguous.

The output is printed so the two languages can be compared exactly - a
translation that is fast but wrong is a failed translation.
"""

MAX_SUBARRAY = '''# Brute-force maximum subarray sum over a seeded pseudo-random sequence.
# O(n^2) by design - the interesting question is whether the model preserves
# the algorithm or silently upgrades it.

def lcg(seed, a=1664525, c=1013904223, m=2**32):
    value = seed
    while True:
        value = (a * value + c) % m
        yield value

def max_subarray_sum(n, seed, min_val, max_val):
    lcg_gen = lcg(seed)
    random_numbers = [next(lcg_gen) % (max_val - min_val + 1) + min_val for _ in range(n)]
    max_sum = float('-inf')
    for i in range(n):
        current_sum = 0
        for j in range(i, n):
            current_sum += random_numbers[j]
            if current_sum > max_sum:
                max_sum = current_sum
    return max_sum

def total_max_subarray_sum(n, initial_seed, min_val, max_val):
    total_sum = 0
    lcg_gen = lcg(initial_seed)
    for _ in range(20):
        seed = next(lcg_gen)
        total_sum += max_subarray_sum(n, seed, min_val, max_val)
    return total_sum

n = 10000
initial_seed = 42
min_val = -10
max_val = 10

import time
start_time = time.time()
result = total_max_subarray_sum(n, initial_seed, min_val, max_val)
end_time = time.time()

print("Total Maximum Subarray Sum (20 runs):", result)
print("Execution Time: {:.6f} seconds".format(end_time - start_time))
'''

PI_LEIBNIZ = '''# Tight floating-point loop. Almost pure interpreter overhead in Python,
# so it shows the raw cost of dynamic dispatch per iteration.

import time

def calculate(iterations, param1, param2):
    result = 1.0
    for i in range(1, iterations + 1):
        j = i * param1 - param2
        result -= (1 / j)
        j = i * param1 + param2
        result += (1 / j)
    return result

start_time = time.time()
result = calculate(100_000_000, 4, 1) * 4
end_time = time.time()

print(f"Result: {result:.12f}")
print(f"Execution Time: {end_time - start_time:.6f} seconds")
'''

PRIME_SIEVE = '''# Sieve of Eratosthenes plus a digit-sum reduction.
# Memory-bound rather than compute-bound, so it stresses a different axis
# than the other two.

import time

def sieve(limit):
    flags = [True] * (limit + 1)
    flags[0] = flags[1] = False
    p = 2
    while p * p <= limit:
        if flags[p]:
            for multiple in range(p * p, limit + 1, p):
                flags[multiple] = False
        p += 1
    return flags

def digit_sum(n):
    total = 0
    while n:
        total += n % 10
        n //= 10
    return total

start_time = time.time()
limit = 5_000_000
flags = sieve(limit)
count = 0
checksum = 0
for n in range(limit + 1):
    if flags[n]:
        count += 1
        checksum += digit_sum(n)
end_time = time.time()

print(f"Primes below {limit}: {count}")
print(f"Digit-sum checksum: {checksum}")
print(f"Execution Time: {end_time - start_time:.6f} seconds")
'''

COLLATZ = '''# Longest Collatz chain below a bound. Unpredictable branching, which
# defeats naive vectorisation and rewards a good compiler.

import time

def chain_length(n):
    steps = 0
    while n != 1:
        n = n // 2 if n % 2 == 0 else 3 * n + 1
        steps += 1
    return steps

start_time = time.time()
limit = 1_000_000
best_n = 0
best_len = 0
for n in range(1, limit):
    length = chain_length(n)
    if length > best_len:
        best_len = length
        best_n = n
end_time = time.time()

print(f"Longest Collatz chain below {limit}: n={best_n} with {best_len} steps")
print(f"Execution Time: {end_time - start_time:.6f} seconds")
'''

EXAMPLES: dict[str, str] = {
    "Maximum subarray sum (O(n^2))": MAX_SUBARRAY,
    "Pi via Leibniz series (100M iterations)": PI_LEIBNIZ,
    "Prime sieve + digit sums (5M)": PRIME_SIEVE,
    "Longest Collatz chain (1M)": COLLATZ,
}

DEFAULT_EXAMPLE = "Maximum subarray sum (O(n^2))"