All write-ups

CTF Write-up · Coding

Special Summation

Summing the odd integers between two bounds while discarding any running total that ends in a zero.

Source: EOman 2022 Category: Coding 35 points Easy

Challenge

Compute the total sum of all odd integers between two given numbers, subject to the condition that any running total ending in a zero is not counted. For the flag, the bounds are 10 and 1000.

7 + 9 = 16 16 + 11 = 27 27 + 13 = 40 (skipped: ends in 0) 27 + 15 = 42 42 + 17 = 59

Analysis

The sequence is accumulated term by term over the odd integers. At each step the candidate total is formed, but it is carried forward only when it does not end in zero. The end-in-zero test is simply the total modulo 10.

Solution

def sum_odd(start, end):
    odds = [x for x in range(start, end) if x % 2 != 0]
    total = 0
    temp = odds[0]
    for i in range(1, len(odds)):
        total = temp + odds[i]
        if total % 10 != 0:
            temp = total
    return total

sum_odd(10, 1000)

The returned value is the flag.

← Back to all write-ups