I am trying to find the number of prime numbers between 2 and n, where n is provided by the user. I can't seem to make it work. here's my code:
>>> def numOfPrime(n):
count = 0
for i in range(2,n):
p = True
for j in range(2,i):
if i % j ==0:
p = False
if p == True:
count += 1
return count
>>> numOfPrime(100)
0
The indentation of your if block is incorrect. It should be in the loop.
def numOfPrime(n):
count = 0
for i in range(2,n):
p = True
for j in range(2,i):
if i % j ==0:
p = False
if p == True:
count += 1
return count
Now numOfPrime(100) returns 25, which correctly accounts for 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, and 97.
Related
I want to write myself a program where a variable is going to increment everytime in the while-loop and want at the end, that all values will be stored in a list. At the end the values of the list should be summed with sum().
My problem is that when I execute my program it just let me show the last number of all. I want to have like l = [5,10,15,...,175] and not just l = [175] (I hope its clear what I mean)
def calc_cost():
x = 0
k = 34
j = 0
while x <= k:
x = x + 1
j = j + 5
l = []
l.append(j)
print(sum(l))
print(calc_cost())
def calc_cost():
x = 0
k = 34
j = 0
l = []
while x <= k:
x = x + 1
j = j + 5
l.append(j)
print(l)
return sum(l)
print(calc_cost())
I made the edit I suggested. I also returned the sum so it could be printed by the line: print(calc_cost())
Here is the output:
[5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 165, 170, 175]
3150
Here's a similar approach using the range function:
def calc_cost():
k = 34
l = list( range( 5, (k+2) * 5, 5 ) )
print(l)
return sum(l)
print(calc_cost())
Can someone point out to me why the list is not returning the list I input with the amount of Even numbers in the list?
import sys
import random
def count_even(num_list):
num_list = [7, 65, 1337, 8, -2, 24, 6, 67, 54, 36, 25, 1, 42, 9, 138, 4356, 6]
count_even = 0
for num in num_list:
if num % 2 == 0:
count_even += 1
return num_list
def main(argv):
error_code = 0
num_list = [7, 65, 1337, 8, -2, 24, 6, 67, 54, 36, 25, 1, 42, 9, 138, 4356, 6]
print(count_even(num_list))
return error_code
if __name__ == '__main__':
error_code = main(sys.argv[1:])
print('[+] Terminated with code: ' + str(error_code))
sys.exit(error_code)
in def count_even, you're returning the array, instead of count of even numbers which is count_even. Like #alaniwi mentioned, replace 'return num_list' with 'return count_even'.
E.g. For the input 5, the output should be 7.
(bin(1) = 1, bin(2) = 10 ... bin(5) = 101) --> 1 + 1 + 2 + 1 + 2 = 7
Here's what I've tried, but it isn't a very efficient algorithm, considering that I iterate the loop once for each integer. My code (Python 3):
i = int(input())
a = 0
for b in range(i+1):
a = a + bin(b).count("1")
print(a)
Thank you!
Here's a solution based on the recurrence relation from OEIS:
def onecount(n):
if n == 0:
return 0
if n % 2 == 0:
m = n/2
return onecount(m) + onecount(m-1) + m
m = (n-1)/2
return 2*onecount(m)+m+1
>>> [onecount(i) for i in range(30)]
[0, 1, 2, 4, 5, 7, 9, 12, 13, 15, 17, 20, 22, 25, 28, 32, 33, 35, 37, 40, 42, 45, 48, 52, 54, 57, 60, 64, 67, 71]
gmpy2, due to Alex Martella et al, seems to perform better, at least on my Win10 machine.
from time import time
import gmpy2
def onecount(n):
if n == 0:
return 0
if n % 2 == 0:
m = n/2
return onecount(m) + onecount(m-1) + m
m = (n-1)/2
return 2*onecount(m)+m+1
N = 10000
initial = time()
for _ in range(N):
for i in range(30):
onecount(i)
print (time()-initial)
initial = time()
for _ in range(N):
total = 0
for i in range(30):
total+=gmpy2.popcount(i)
print (time()-initial)
Here's the output:
1.7816979885101318
0.07404899597167969
If you want a list, and you're using >Py3.2:
>>> from itertools import accumulate
>>> result = list(accumulate([gmpy2.popcount(_) for _ in range(30)]))
>>> result
[0, 1, 2, 4, 5, 7, 9, 12, 13, 15, 17, 20, 22, 25, 28, 32, 33, 35, 37, 40, 42, 45, 48, 52, 54, 57, 60, 64, 67, 71]
I have made a piece of code that spits out prime numbers up to the 10001st number. It currently takes up 4 lines of code, and was wondering if I could condense it further? Here it is;
for i in range(3,104744,2):
for x in range(3,int(i/2),2):
if i % x == 0 and i != x: break
else: print(i)
I am aware that condensing code too much is usually not a good thing, but was wondering if it was possible.
Thanks.
You can use a list comprehension and any to get a one-liner solution:
>>> [p for p in range(2, 100) if not any (p % d == 0 for d in range(2, int(p**0.5) + 1))]
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
It uses the fact that a divisor cannot be larger than the square root of the number it divies.
It seems to work fine:
>>> len([p for p in range(2, 104744) if not any (p % d == 0 for d in range(2,int(p**0.5)+1))])
10001
List comprehension
>>> r=range(2,100)
>>> [p for p in r if [p%d for d in r].count(0)<2]
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
Try this one:
for i in range(3,100,2):
if all( i%x for x in range(3, i//2, 2) ):
print(i)
I want to make a program on Python that will add 5 per count until count is 20, so the total would be 100. So basically I want to show the result of 5 * 20 using this way.
num = 5
count = 0
total = 0
I tried this code but it returns as zero. Why?
while(count == 20):
total = num * count
if(total == num * count):
count = count + 1
print total
Please fix any mistake I made. I'm new to Python...
You probably meant while count <= 20:
The condition specified for a while loop is what needs to be true for it to keep running - not for when it ends.
Also note that you don't need parentheses around the while and if conditions.
Your code also has some odd redundancies, though.
For instance:
total = num * count
if total == num * count:
count = count + 1
The if statement will always be true, given that you're setting total to the same thing you check it against, in the previous line. In other words, you could have just written...
total = num * count
if True:
count = count + 1
or even just...
total = num * count
count = count + 1
Furthermore...
You set total equal to num * count on each iteration, but if your goal is just to print out num * 20, you don't have to count up to 20 - you can just start out at 20.
num = 5
count = 20
print num * count
Also note...
that this line can be more concisely stated:
count = count + 1
can also just be written as...
count += 1
Finally...
If what you really wanted was a list of numbers in increments of 5 up to 100, you could do this:
>>> range(0, 101, 5)
[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]
or this:
>>> [n*5 for n in range(21)]
[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]