Different output in for loop - python-3.x

I made a simple program to generate prime numbers but It shows 10 as a prime number. In debugging the for loop incremented i from 2 to 4 in single step when number went from 9 to 10.
for i in range (2, int( number / 2) + 1):
if number % i == 0:
number += 1
i = 2
yield number
number += 1`

Related

Given a number N, print sum of all even numbers from 1 to N. Using While loop in python

number = int(input())
count = 2
sum = 0
while count <= number:
if count%2 == 0:
sum = sum + count
count = count + 1
print(sum)
I did this but still, the code is wrong.
Given the code above, your main issue is indentation:
number = int(input())
count = 2
sum = 0
...
count and sum should start where number starts as well:
number = int(input())
count = 2
sum = 0
...
Also, you could make your code more efficient by incrementing your count by 2, therefore getting rid of the if statement:
number = int(input())
count = 2
Sum = 0
while count <= number:
Sum += count
count += 2
print(Sum)
And of course, as Alex P said, avoid using sum as a variable name as it is bad practice. Use Sum, or any other variation that doesn't look like the method sum().

why using variable and a number in loop show different outcomes

a = 1
for number in range(5):
a = a + number * 5
print(a)
#
for number in range(5):
a = 1 + number * 5
print(a)
In the first loop, you retain the value of a, and add to it number * 5 in each iteration.
In the second loop, you always take 1 and add to it number * 5.
You can, of course, achieve the same behavior with variables, you just need to use another one:
first = 1
for number in range(5):
a = first + number * 5
print(a)

python matrix multiplication check if number of rows of 1st matrix is equal to number of columns of 2nd matrix

I need to perform a matrix multiplication between 2 matrices by taking user input. The below code works fine for the multiplication part but if the no. of rows of 1st matrix are not equal to the no. of columns of the 2nd matrix then it should print NOT POSSIBLE and exit. But it still goes on to add the elements of the matrices. What could possibly be wrong in this code & what could be the solution for the same. Any help would be greatly appreciated
def p_mat(M,row_n,col_n):
for i in range(row_n):
for j in range(col_n):
print(M[i][j],end=" ")
print()
def mat_mul(A_rows,A_cols,A,B_rows,B_cols,B):
if A_cols != B_rows:
print("NOT POSSIBLE")
else:
C = [[0 for i in range(B_cols)] for j in range(A_rows)]
for i in range(A_rows) :
for j in range(B_cols) :
C[i][j] = 0
for k in range(B_rows) :
C[i][j] += A[i][k] * B[k][j]
p_mat(C, A_rows, B_cols)
if __name__== "__main__":
A_rows = int(input("Enter number of rows of 1st matrix: "))
A_cols = int(input("Enter number of columns of 1st matrix: "))
B_rows = int(input("Enter number of rows of 2nd matrix: "))
B_cols = int(input("Enter number of columns of 2nd matrix: "))
##### Initialization of matrix A and B #####
A = [[0 for i in range(B_cols)] for j in range(A_rows)]
B = [[0 for i in range(B_cols)] for j in range(A_rows)]
print("Enter the elements of the 1st matrix: ")
for i in range(A_rows):
for j in range(A_cols):
A[i][j] = int(input("A[" + str(i) + "][" + str(j) + "]: "))
print("Enter the elements of the 2nd matrix: ")
for i in range(B_rows):
for j in range(B_cols):
B[i][j] = int(input("B[" + str(i) + "][" + str(j) + "]:"))
##### Print the 1st & 2nd matrices #####
print("First Matrix : ")
p_mat(A,A_rows,A_cols)
print("Second Matrix : ")
p_mat(B,B_rows,B_cols)
### Function call to multiply the matrices ###
mat_mul(A_rows,A_cols,A,B_rows,B_cols,B)
For matrix multiplication, the number of columns in the first matrix must be equal to the number of rows in the second matrix.
If you want to check the no of rows of 1st matrix and the no. of columns of the 2nd matrix then change the if A_cols != B_rows to if A_rows != B_cols
With your current code, it will print NOT POSSIBLE when A_cols != B_rows which is right.
Ex.
Enter number of rows of 1st matrix: 2
Enter number of columns of 1st matrix: 3
Enter number of rows of 2nd matrix: 2
Enter number of columns of 2nd matrix: 3
Enter the elements of the 1st matrix:
A[0][0]: 1
A[0][1]: 2
A[0][2]: 3
A[1][0]: 4
A[1][1]: 5
A[1][2]: 6
Enter the elements of the 2nd matrix:
B[0][0]:1
B[0][1]:2
B[0][2]:3
B[1][0]:4
B[1][1]:5
B[1][2]:6
First Matrix :
1 2 3
4 5 6
Second Matrix :
1 2 3
4 5 6
NOT POSSIBLE
Another mistake in the code is when you are initialize the Matrices.You are doing
A = [[0 for i in range(B_cols)] for j in range(A_rows)]
B = [[0 for i in range(B_cols)] for j in range(A_rows)]
If the B_cols are smaller than the A_cols when you adding elements in A it will raise IndexError
The same if the B_cols are greater than A_cols when you are adding elements to B will raise IndexError.
Change it to
A = [[0 for i in range(A_cols)] for j in range(A_rows)]
B = [[0 for i in range(B_cols)] for j in range(B_rows)]

pascal number using while loop

I need to print the pascal number
1
1 1
1 2 1
1 3 3 1
etc.
import math
i = 0
j = 1
while j<6:
while i<6:
print(int(math.factorial(5)/(math.factorial(i)*math.factorial(5-i))), end=" ")
i += 1
print(int(math.factorial(j)/(math.factorial(i)*math.factorial(j-i))))
j += 1
it is stated that factorial cannot be negative, although I don't think it is negative.
Take a look at this code, this gives the correct output, even though you can work on making it pretty
n = 5
for j in range(1, n + 1):
row = 1
for i in range(1, j + 1):
print(row)
row = row * (j - i) // i
print(" ")
We know that ith entry in a row of the triange is binomial coefficiant of (j, i) and that all the rows must start with the number 1, and that's why this works. And ofcourse we do integer division.
The for loops can be replace with while loops as you see fit.

Triangle of odd numbers

I'm trying to make a triangle of number like this one here
1
12
123
12
1
Number of line is chosen by the user. For example if user enters : 5. There must be 5 line (like in the previous triangle). And the number must be an odd number.
Here is what I have done so far but i'm stuck.
oddNumber = int(input("Enter an odd number here:"))
for i in range(oddNumber):
for j in range(oddNumber):
print("", end="")
for k in range(1,i):
print(k, end="")
print("")
The output of that is (if I enter 5):
1
12
123

Resources