Skip to content

Instantly share code, notes, and snippets.

@tusharhero
Last active March 12, 2022 12:11
Show Gist options
  • Save tusharhero/90899a8e0a90199f919eccbaa93894fe to your computer and use it in GitHub Desktop.
Save tusharhero/90899a8e0a90199f919eccbaa93894fe to your computer and use it in GitHub Desktop.
finds sum of all odd numbers between 1 to n
#! /bin/python3
'''
WRITTEN by Tushar Maharana
LICENSE: https://tusharhero.mit-license.org/
Python program to find sum of all odd numbers between 1 to n.
'''
def is_it_odd(n):#checks if the input is odd
n = int(n)
m = int(n/2)
if 2*m == n:#haha math go brr
return 0
else:
return 1
def generate_n_nums_list(n):#generate a list of numbers
n = int(n)
l = 1#counter
n_list = []
while l < n:
n_list.append(l)
l = l+1
return n_list
def generate_odd_nums_list(nstr):
l = 0
odd_list = []
while l < len(nstr):
cn = is_it_odd(nstr[l])
if cn == 1:#if odd add
odd_list.append(nstr[l])
l = l + 1
else:
l = l + 1
return odd_list
def sum_of_number_strings(odd_list):
sum_of_number_strings = 0
l = 0
while l < len(odd_list):# keep adding
sum_of_number_strings = sum_of_number_strings + int(odd_list[l])
l = l + 1
return sum_of_number_strings
#driver
n = input("enter n:")
print(sum_of_number_strings(generate_odd_nums_list(generate_n_nums_list(n))))
@tusharhero
Copy link
Author

Maybe I could add a separete function to mix all this i forgor to do that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment