Python prime number generator prints out every number - python

I made this prime number generator but it just prints every number (1, 2, 3...).
I am aware that there are other questions about this but I don't understand any of them. Here is what I have so far:
x = 1
y = 1
while x >= y:
if x % y == 0:
print(x)
x = x + 1
y = 1
else:
y = y + 1
y = 1

From your question, I think, it would be better try this way :
n = 10
i = 2
while i < n :
prime = 1 # reset the `prime` variable before the inner loop
for a in range(2, i):
if i%a == 0:
prime = 0
break
if prime:
print (i)
i += 1
Output :
2
3
5
7

There's something called a rubber ducky test where you explain your code, step by step, to a rubber duck. It forces you to walk through every step and you'll often find the error. In this case, think about what y is when you do x%y. You'll see that y=1 the first time this is called. So it will iterate through the loop incrementing x by 1 and y will remain set it 1. And x%y when y=1 will always be...

Related

Write a program using while loop, which prints the sum of every fifth number from 0 to 500 (including both 0 and 500)

This is what I have done:
Can anybody tell me where I went wrong?
num = 0
x = 1
while x <= 500:
num += x
x += 5
print(num)
Your indentation is also wrong. It needs to be like this
num = 0
x = 0
while x <= 500:
num += x
x += 5
print(num)
But, if you want to print only the final sum, you can use the print statement outside loop
num = 0
x = 0
while x <= 500:
num += x
x += 5
print(num)
Logic: We have to use while loop to check every number. We have to check whether the number is divisible by 5 or not, for that, we have to put if condition and keep on adding +1 to go from 0 to 500. If the condition is satisfied, add to to a variable whose initial value is 0. You code:
x=0
r=0
while x<=500:
if x%5==0:
r+=x
x+=1
print(r)
Or you can do it this way, start the initial value from 0, updating value of x should be 5, add it to variable whose initial value is 0. You alternative code:
x=0
r=0
while x<=500:
r+=x
x+=5
print(r)
The second code will help you rectify your personal code. Have a look
Yet another way:
x = 0
acc = 0
while (x := x + 5) <= 500:
acc += x
Here we use an assignment operator inside while() to get the value while incrementing it at the same time.

Python: print even numbers including 0

Given a program that outputs all the numbers from 0 to 10, I must print only the even numbers. I'm using this code:
x = 0
while x <= 10:
x += 1
if x % 2 == 0:
print(x)
The output shows numbers from 2 to 10, excluding 0, which I need to include.
How can I print even numbers from 0 to 10?
Thanks!
** Note: I'm a beginner, so any explanation is extremely helpful.
x = 0
while x <= 10:
if x % 2 == 0:
print(x)
x += 1 # increment x AFTER the if statement, otherwise you never check if 0%2 == 0
You could also use a for-loop to simplify things further:
for x in range(11):
if x%2 == 0:
print(x)
As others have said, the problem is that you're incrementing x before you check if it's even, so you increment from 0 to 1 before printing.
But there's no need for the test, you can just loop over the even numbers:
for x in range(0, 11, 2):
print(x)
The third argument to range() is the steps, and stepping by 2 just returns even numbers.
Increment x after the if block.
hmmm....can you ask quiz questions on here? anyhow, I think the small edit to the order of operations in your loop should do it?
x = 0
while x <= 10:
if x % 2 == 0:
print(x)
x += 1
This can be esaily done with the for loop:
for i in range(11):
if i % 2 == 0:
print(i)
and that's all it takes, 3 lines :)
Its fine!
x = 0
while x <= 10:
if x % 2 == 0:
print(x)
x += 1
A slightly different slant on a on-liner ... just for fun.
print(*(i for i in range(0, 11, 2)))
>>> 0 2 4 6 8 10

How do you mulitply via addition?

I'm having trouble trying to
compute x * y using only addition, printing the intermediate result for each loop iteration, and
printing the final number. This is my
teacher's example of what the output should look like
I could really use some help figuring it out,, I don't really understand how to yet.
Edit: I am a first time coder so I'm not very skilled, I do know the basics though (Also very new to this website) Here's what my code looks like so far:
x = int(input("Input positive x: "))
y = int(input("Input positive y: "))
z = 0
w = 0
if x < 0:
exit("Please input a positive number for x")
if y < 0:
exit("Please input a positive number for y")
def multiplier(number, iterations):
for i in range(1, iterations):
number = number + 3
print(number) # 12
multiplier(number=3, iterations=4)
My answer. Little shorter than the others.
Ok, try thinking of multiplication as adding a number with itself by x times. Hence, 3*4 = 3+3+3+3 = 12, which is adding 3 by itself 4 times
Replicating this with a for loop:
#inputs
x = 3
y = 4
#output
iteration = 1
result = 0
for num in range(4):
result += x
print(f'Sum after iteration {iteration}: {result}')
iteration += 1 #iteration counter
The resulting output will be:
Sum after iteration 1: 3 Sum after iteration 2: 6 Sum after iteration
3: 9 Sum after iteration 4: 12
The code is simple and straightforward,Check which number is bigger and iterate according to that number.
x = 3
y = 4
out = 0
if x > y:
for v in range(0,x):
out += y
print("after iteration",v+1, ":", out)
if y > x:
for v in range(0,y):
out += x
print("after iteration",v+1, ":", out)
print(x,"*", y, "=", out)
Use print to print inside the loop to show each number after adding and the number of iteration occurs.
Here is a start. This assume both arguments are integers 0 or more:
def m(n1, n1a):
n1b = 0
while n1a > 0:
n1b += n1
n1a -= 1
return n1b
n = m(9, 8)
print(n == 72)

Why are these lines of code in python only outputting the same answer?

I'm trying to get this program to return all possible multiples of 3 and 5 below 1001 and then add them all together and print it but for some reason these lines of code only seem to be printing one number and that number is the number 2 which is obviously wrong. Can someone point me in the right direction to why this code is grossly wrong?
n = 0
x = n<1001
while (n < 1001):
s = x%3 + x%5
print s
You've got a few mistakes:
x is a boolean type
Your loop never ends
adding values to mimic lists?
Edit
Didn't see the part where you wanted sum, so you could employ a for-in loop or just a simple one like so:
sum = 0
for i in range(1001):
if(i % 3 == 0 or i % 5):
sum += i
print(sum)
(Python 3)
You need to stop while at some point by incrementing n. Here is some code:
nums = []
n = 0
while (n < 1001):
# in here you check for the multiples
# then append using nums.append()
n += 1
This creates a loop and a list that accounts for every number in 0 to 1000. In the body, check for either condition and append to the list, then print out the values in the list.
num is a list where you are going to store all the values that apply, (those numbers who are divisible by 3 and 5, which you calculate with modulo). You append to that list when a condition is met. Here is some code:
nums = []
n = 0
while (n < 1001):
if(n % 3 == 0 or n % 5 ==0):
nums.append(n)
n += 1
print(n) #or for loop for each value
Explanation: a list of numbers called num stores the numbers that are divisible by 3 or 5. The loop starts at zero and goes to 1000, and for all the values that are divisible by 3 or 5, they will be added to the list. Then it prints the list.
Of course there is a simpler approach with a range:
for i in range(1001):
if(i % 3 == 0 or i % 5 == 0):
print(i)
This will print out all the values one by one. It is 1001 because the upper limit is exclusive.
true=1
false=0
so:
x = n<1001
we have x=1 because 0<1001 is true
s = x%3 + x%5
the remainder of 1/3 is 1 and 1/5 is 1
In your code:
1. x=n<1001 - generates a boolean value; on which we can't perform a mathematical operation.
In while loop:
your variable n,x are not changing; they are constant to same value for all the iterations.
Solution 1:
Below code will help you out.
s=0
for i in range(1,1002):
if( i%3 == 0 or i%5 == 0):
s = s + i
print(s)
Solution: 2
There is one more approach you can use.
var = [i for i in range(1,1002) if i%3==0 or i%5 ==0]
print(sum(var))

How to stop repetition in my code?

This code executes primes upto a given number. It works correctly as far as the prime generation is concerned but the output is painfully repetitive.
The code is:
numP = 1
x = 3
y = int(input('Enter number of primes to be found: '))
while numP<=y:
for n in range(2, x):
for b in range(2, n):
if n % b == 0:
break
else:
# loop fell through without finding a factor
print (n)
x += 2
numP += 1
The output is like (e.g. for y=4):
2
2
3
2
3
5
2
3
5
7
I want to avoid repetition and obtain output like:
2
3
5
7
How should the code be modified?
If you store the data in a set, you will avoid repetitions
There's no reason to loop n from 2..x. Get rid of that loop, and replace all references to 'n' with 'x'. I.e.:
def find_primes(y):
x = 3
numP = 0
while True:
for b in range(2, x):
if x % b == 0:
break
else:
# loop fell through without finding a factor
print (x)
numP += 1
if numP >= y:
return
x += 2

Categories

Resources