Programs of Loops
Find prime numbers and traverse arrays in a circular manner.
We'll cover the following
Store the first n
prime numbers in the array
The following program stores the first n
prime numbers to an array. The value of n
is chosen by the user, and any number can be used. Here’s how to solve this problem:
- Start with an empty array of
n
elements. - Store as the first prime number at the index.
- Keep checking every following integer by dividing it by every prime value in the array.
- Check by taking the remainder after dividing it by all the prime values in the array. The only value in the current array is . Since the remainder is non-zero, is stored in the array.
- Then, check in the same way. Since the remainder of divided by is , it’s not stored in the array.
n = 5 # The number of prime numbers to be storedif n < 1 # Checks for the validity of inputprint("It seems the number of desired prime numbers has got an invalid value:",n,"\n")elsep = [2]*n # Generate a array to store n values with initial value of 2 for all elementspcount = 0v = p[pcount]+1 # The value to be testedwhile pcount < n-1 # Loop for number of generated valuesr = pcount+1i = 0pflag = 0while i <= pcount and pflag == 0 # Loop for testing v against each prime number p[i]if v % p[i] == 0v += 1 # Current v is not a prime number, generate next value to be testedpflag = 1 # Flag to stop the inner loopelsei += 1 # Move forward the index in the array of prime numbersendendif pflag == 0 # Check flag after finishing the inner looppcount += 1 # Increment pcount, we found next prime numberp[pcount] = v # Store the next prime number at next position in the arrayendendprint(p)end
In the above code:
- The variable,
n
, shows the total number of prime numbers. - The first
if
statement specifies that the value ofn
shouldn’t be less than1
. - We create an array of
n
values,p = [2] * n
. The syntax[2] * n
is used to create an array ofn
elements, each having a value of2
. - We use two nested
while
loops.- The outer loop counts the number of generated prime numbers.
- The inner loop keeps generating sequential integers until the next prime number is found.
- We use the following variables:
-
n
stores the total number of prime numbers. -
p
stores the generated prime numbers. -
pcount
keeps track of the most recently generated prime number. -
v
stores the next number to be tested. -
i
iterates through the array of prime numbers generated so far -
pflag
indicates whether the current number is prime.
-
The comments in the program itself will help explain the rest of the code.
Circular traversal
The following program demonstrates circular moves in an array using the positive numbers in that array. The program makes the value of the cell that is shown negative and ends when all values are negative.
To solve this problem, we first need to understand circular traversals.
a = [ 2 , 8 , 3 , 15 ] # Enter the array for circular traversali = 0j = 0allneg = 0addneg = 0while allneg == 0 # This loop will terminate when the value of allneg is not zeroif a[i] > 0 # If the value of a is greater than zeroprint("Now PROCESSING the index ",i,"\n")elseif (i != j) # If i is not equal to jprint("Now skipping the index ",i,"\n")elseprint("Now STOPPING at the index ",i,"\n")endendprint(i,a,"\n")if (a[i] > 0) # If the value of a is greater than zeroa[i] = -a[i] # Converting value to negative valuek = 0ii = i # Storing value of i in iiprint("Moving ",-a[i]," steps","\n")while k < -a[i] # This loop will terminate when k is not less than -a[i]ii = (ii + 1) % a.lengthk += 1print(". ",ii,a,"\n")endi = ii # Storing the updated value of ii to iaddneg = 1else # If the value of a is not greater than zeroif addneg == 1 # If the value of addneg is 1j = iaddneg = 0 # Change the value of addneg to 0elseif (j == i) # If i is equal to jallneg = 1endendi = (i + 1) % a.length # Update value of iendendprint(" *** DONE *** ")
Calculate the product of two matrices
Write a program to display the product of these two matrices and , resulting in a matrix :
Sample input
a =[ [1,2,3,4] ,
[5,6,7,8] ,
[9,10,11,12] ]
b =[ [10,20] ,
[30,40] ,
[50,60] ,
[70,80] ]
Sample output
Matrix Product:
[[500, 600], [1140, 1400], [1780, 2200]]
a = [ [1,2,3,4] , [5,6,7,8] , [9,10,11,12] ]b = [ [10,20] , [30,40] , [50,60] , [70,80] ]aROWS,aCOLS = 3,4bROWS,bCOLS = 4,2cROWS,cCOLS = 3,2c = [[0,0],[0,0],[0,0]]for i in 0...aROWSfor j in 0...aCOLSfor k in 0...bCOLSc[i][k] += a[i][j] * b[j][k]endendendprint("Matrix Product:\n",c)
In the program above:
- We create two matrices,
a
andb
. - We create variables (
aROWS
,aCOLS
,bROWS
,bCOLS
,cROWS
, andcCOLS
) and assign them values (3
,4
,4
,2
,3
, and2
,respectively). - We create a matrix,
c[3][2]
, initialized with zeros. - We calculate the product of matrices
a
andb
and store the result inc
using three nested loops. - We display the resultant matrix,
c
, directly (as a multi-dimensional array) without using loops.