top of page
Prostate Gland releases PSA molecules (15).png

CONDITIONS AND LOOPS

Problem:

Given:  Two positive integers a and b  (a < b < 10000).

​

Return: The sum of all odd integers from a through b, inclusively.

​

Sample Dataset: 100   200

​

Sample Output:  7500

Solution:

This problems requires the use of a for loop, and the for loop can be implemented in a couple of different ways to get the same answer (obviously there are different ways to tackle any problem using Python). I used the modulus operator to find whether a number is divisible by 2 and if it wasn't, added it to the sum:

< >

a = 100

b = 200

​

sum = 0

​

for number in range(a, b+1):

if number % 2 != 0:

sum += number

​

print(sum)

Output:

7500

You could try adding a step to the range function instead of using the modulus.

Copy and paste the output you get from running your code on the sample data onto the Rosalind answer terminal and submit. Annnd get yourself a cookie!

Subscribe to my mailing list and never miss a blogpost or new puzzle!

  • White Instagram Icon

Thanks for submitting!

bottom of page