Adding together elements contained in two lists - python-3.x

I am trying to add elements of two lists together e.g. listing all integers below 10 that are multiples of 3 or 5. The sum of these multiples should be 23 (3+5+6+9) but I keep getting 18. I have only just starting programming and learning python. Here's my code:
for i in range (1,10):
if i % 3 == 0:
print(i)
for x in range (1,10):
if x % 5 ==0:
print(x)
sum_multiples=i+x
print(sum_multiples)

What you are trying to do:
3 + 6 + 9 + 5 = 23
What you are doing:
9 + 9 = 18
This is what you want:
l1 = [x for x in range(1, 10) if x % 3 == 0]
l2 = [x for x in range(1, 10) if x % 5 == 0]
print(sum(l1) + sum(l2))

First off, let me address your problem. When you add variable i and variable x, they are both value of 9. This is because after the loop their value is 9, and because you are adding the values after the loop, you are adding 9 + 9.
You can fix this by making a variable outside the loop and adding the number to the variable. My code would be:
total = 0
for i in range (1,10):
if i % 3 == 0:
print(i)
total = total + i
for x in range (1,10):
if x % 5 ==0:
print(x)
total = total + x
print(total)
(this is just based off what you were doing, I would probably condense it into one loop)

You could so something like this:
sum_multiples = 0
for i in range(1, 10):
if i % 3 == 0 or i % 5 == 0:
sum_multiples += i
print(sum_multiples)
If you prefer a one line solution:
print(sum(i for i in range(1, 10) if i % 3 == 0 or i % 5 == 0))

Related

Sum function not working when trying to add numbers in lists using python

I am trying to create a program that takes only even numbers from a range of numbers from 1 to 100 and add all of the even numbers. I am a beginner and I have been trying to get this working since yesterday, and nothing I have tried works. This is my first post, so sorry if the format is wrong but here is my code.
for i in range(1, 100):
if i % 2 == 0:
x = [I]
y = sum(x)
print(y)
The problems with your code has multiple issues that - 1)if you want to get all even numbers from 1 to 100, your range should be (1, 101); 2) the way you build list is wrong (syntax); 3) the sum expect an iterable (list).
There are a few ways to accomplish this sum from 1 to 100 (inclusive), here it will start with yours, and try to show List Comprenshion and Generator Expression way:
lst = [] # to store the build-up list
tot = 0 # to store the answer
for i in range(1, 101):
if i % 2 == 0: # it's a even number
lst.append(i) # store it into lst
tot = sum(lst) # 2550
Generator expression:
all_evens_sum = sum(x for x in range(1, 101) if x % 2 == 0) # 2550
Or List Comprehension:
lst = [x for x in range(1, 101) if x % 2 == 0] # even nums
total = sum(lst) # 2550
I am a beginner too but it looks I can help you some.
for i in range(1, 100):
if(i % 2 == 0):
sum += i
print(sum) // do what you want

FIND THE PERFECT NUMBER IN PYTHON

I do have an answer already but I'm curious if there's a better method on coding this problem through python. Thank you.
A number is called a perfect number if it is equal to the sum of all of its divisors, not including
the number itself. For instance, 6 is a perfect number because the divisors of 6 are 1, 2, 3, 6
and 6 = 1 + 2 + 3. As another example, 28 is a perfect number because its divisors are 1, 2, 4,
7, 14, 28 and 28 = 1 + 2 + 4 + 7 + 14. However, 15 is not a perfect number because its divisors
are 1, 3, 5, 15 and 15 6= 1 + 3 + 5. Write a program that finds all four of the perfect numbers
that are less than 10000.
Here is my answer.
for i in range(1,10000):
summ = 0
for m in range(i):
if i % (m+1) == 0:
summ = summ + (m+1)
g = summ - i
if g == i :
print(g)
Here is perfect number algorithm that checks for perfect numbers using the Lucas Lehmer Test:
def ffs(x):
return (x&-x).bit_length()-1
def levelx(N, withstats=False):
if N <= 1: return False
temp = N
newlevel = temp.bit_length()
primetest = temp//(2**(ffs(temp)))
offset = temp//primetest
s = 4
nextlevel = newlevel // 2
check = temp // (2**nextlevel)
prevtemp = 2**nextlevel
if withstats == True:
print (newlevel, newlevel, temp)
print (newlevel, nextlevel+one, prevtemp)
if (prevtemp & (prevtemp-1) == 0) and prevtemp != 0:
if offset.bit_length() == primetest.bit_length():
if ((primetest+1) & ((primetest+1)-1) == 0):
for x in range(primetest.bit_length()-1):
s = (s * s - 2) % primetest
if withstats == True:
print(s)
if s in [0,2]: return True
return False
else: return False
else: return False
else: return False
Here is the output:
In [645]: for x in range(2,100):
...: if levelx((2**(x-1))*(2**x-1)) == True:
...: print(x, (2**(x-1))*(2**x-1))
...:
...:
2 6
3 28
5 496
7 8128
13 33550336
17 8589869056
19 137438691328
31 2305843008139952128
61 2658455991569831744654692615953842176
89 191561942608236107294793378084303638130997321548169216
You can use gmpy2 to speed these up by about 10x.
for j in range(1,10000):
sum=0
for I in range(1,J):
if J%I==0:
sum= sum + I
if j==sum:
print(J," is a perfect square")

Finding sum of n odd numbers following a given integer

Read an integer N that is the number of test cases that follows. Each test case contains two integers X and Y. Print one output line for each test case that is the sum of Y odd numbers from X including it if is the case. For example: for the input 4 5, the output must be 45, that is: 5 + 7 + 9 + 11 + 13 for the input 7 4, the output must be 40, that is: 7 + 9 + 11 + 13
Try this I was just working on it.
x = int(input("Enter Value of X: "))
y = int(input("Enter Value of Y: "))
result = 0
itt = 0
while itt != y:
if x % 2 == 0:
x += 1
else:
itt += 1
result += x
x += 1
print(result)

Prime number generator returns none at the end when inside a function sometimes

I made this prime number generator in python:
for x in range(a , b):
y = 2
while 1 == 1:
if x % y == 0:
if x == y:
print(x)
break
else:
break
y += 1
and it returns all prime numbers up to b as long as a > 2 and b > 3
for example, when a = 2 and b = 11 i get this
2
3
5
7
but when I nest it inside a function like this and print it:
def f(b,a=2):
for x in range(a , b):
y = 2
while 1 == 1:
if x % y == 0:
if x == y:
print(x)
break
else:
break
y += 1
print(f(11))
I get this:
2
3
5
7
None?
Why is it printing a none while inside a function and not without? And how would I fix this?

Curl inside while loops 1

what is the difference between this code
x = 10
while x:
x = x - 1
if x % 2 == 0:
print(x, end = ' ')
and this code
x = 10
while x:
if x % 2 == 0:
x = x - 1
print(x, end = ' ')
I'v just start the python lerning thanks in advance
In the first code block:
You subtract 1 from x after every loop iteration, thus the value of x will be:
10 (outside the loop)
9
8
...
1
0
It will print x whenever x is even
8 6 4 2 0
The second code block will loop infinitely after the first iteration.
The inside of the if statement is executed only one when x = 10. After that, the value of x is always 9 and it stays like that infinitely.

Resources