Last Updated: June 7, 2026
Add together every number of a chosen parity in the range from 1 up to n, including n when it qualifies. When parity is "even", only values that 2 divides cleanly are added: 2, 4, 6, and so on. When it is "odd", only values that leave a remainder of 1 are added: 1, 3, 5, and so on.
When no number in the range matches the requested parity, the sum is 0. That happens for the even case when n is 0 or 1, since the range holds no even number. A correct solution returns 0 there.
The two parities are mirror images, so a single piece of code handles both once it knows which remainder to look for. Each approach below reads the parity once and then applies the same logic.
0 <= n <= 10000 → A loop from 1 to n runs at most 10000 iterations, which is fast. A plain int holds the result without trouble.parity is always "even" or "odd" → There are only two cases to handle, and they differ by a single value: the remainder to match, or the number to start counting from.n = 10000, the even total is 25005000 and the odd total is 25000000, both well under the int limit. Larger ranges would need a 64-bit type.Visit each number from 1 to n and add it to a running total whenever its parity matches. The modulo operator % reports a number's parity: i % 2 is 0 for even values and 1 for odd values.
Translate the parity string into a target remainder once, then add i to the total whenever i % 2 equals that target. The same loop handles both requests.
target to 0 when parity is "even", or 1 when it is "odd".sum at 0.i from 1 to n inclusive.i % 2 == target, add i to sum.sum.Input:
The parity is "even", so the target remainder is 0. Walking from 1 to 10, the loop skips the odd values and adds the even ones. It adds 2 to reach 2, then 4 to reach 6, then 6 to reach 12, then 8 to reach 20, and finally 10 to reach 30. The numbers 1, 3, 5, 7, and 9 contribute nothing. The final total is 30.
n.n.The first approach inspects every number, including the ones it discards. The matching numbers form a sequence on their own: even numbers are 2, 4, 6, odd numbers are 1, 3, 5. Both step forward by 2 and differ only in where they start.
Instead of testing each value, start at the right point, 2 for even or 1 for odd, then step forward by 2 and add every value the loop touches. This removes the modulo check from the loop body and halves the iterations.
start to 2 when parity is "even", or 1 when it is "odd".sum at 0.i at start and, while i <= n, add i to sum and increase i by 2.i passes n, return sum.Input:
The parity is "odd", so the loop starts i at 1 and adds it, giving 1. It moves to 3 and adds it, reaching 4. It moves to 5 and adds it, reaching 9. It moves to 7 and adds it, reaching 16. It moves to 9 and adds it, reaching 25. The next value would be 11, which is greater than 9, so the loop stops. The total is 25.
n.Each parity has a closed form that gives the answer outright, so the code branches once and computes the result with arithmetic.
For the even case, the count of even numbers between 1 and n is k = n / 2 using integer division. Those numbers are 2, 4, 6, up to 2k. Factoring out the 2 turns their sum into 2 * (1 + 2 + ... + k), and since 1 + 2 + ... + k equals k * (k + 1) / 2, the even sum simplifies to k * (k + 1).
For the odd case, the count of odd numbers is k = (n + 1) / 2. The sum of the first k odd numbers is always k squared. Adding them step by step shows why: 1 is 1², then 1 + 3 is 4 which is 2², then 1 + 3 + 5 is 9 which is 3², and each new odd number extends the square by one side. So the odd sum is k * k.
Both branches use a single division and one or two multiplications, no loop required.
parity is "even", compute k = n / 2 and return k * (k + 1).parity is "odd", so compute k = (n + 1) / 2 and return k * k.Input:
The parity is "even", so the even branch runs. Integer division gives k = 10 / 2 = 5, the count of even numbers in the range. Applying the formula, the sum is k * (k + 1) = 5 * 6 = 30. No loop runs; one multiplication produces the answer.
n.k and the result are stored.