Programs of Loops

Find prime numbers and traverse arrays in a circular manner.

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 22 as the first prime number at the 0th0^{th} index.
  • Keep checking every following integer by dividing it by every prime value in the array.
    • Check 33 by taking the remainder after dividing it by all the prime values in the array. The only value in the current array is 22. Since the remainder is non-zero, 33 is stored in the array.
    • Then, check 44 in the same way. Since the remainder of 44 divided by 22 is 00, it’s not stored in the array.
Press + to interact
n = 5 # The number of prime numbers to be stored
if n < 1 # Checks for the validity of input
print("It seems the number of desired prime numbers has got an invalid value:",n,"\n")
else
p = [2]*n # Generate a array to store n values with initial value of 2 for all elements
pcount = 0
v = p[pcount]+1 # The value to be tested
while pcount < n-1 # Loop for number of generated values
r = pcount+1
i = 0
pflag = 0
while i <= pcount and pflag == 0 # Loop for testing v against each prime number p[i]
if v % p[i] == 0
v += 1 # Current v is not a prime number, generate next value to be tested
pflag = 1 # Flag to stop the inner loop
else
i += 1 # Move forward the index in the array of prime numbers
end
end
if pflag == 0 # Check flag after finishing the inner loop
pcount += 1 # Increment pcount, we found next prime number
p[pcount] = v # Store the next prime number at next position in the array
end
end
print(p)
end

In the above code:

  • The variable, n, shows the total number of prime numbers.
  • The first if statement specifies that the value of n shouldn’t be less than 1.
  • We create an array of n values, p = [2] * n. The syntax [2] * n is used to create an array of n elements, each having a value of 2.
  • 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.

Press + to interact
a = [ 2 , 8 , 3 , 15 ] # Enter the array for circular traversal
i = 0
j = 0
allneg = 0
addneg = 0
while allneg == 0 # This loop will terminate when the value of allneg is not zero
if a[i] > 0 # If the value of a is greater than zero
print("Now PROCESSING the index ",i,"\n")
else
if (i != j) # If i is not equal to j
print("Now skipping the index ",i,"\n")
else
print("Now STOPPING at the index ",i,"\n")
end
end
print(i,a,"\n")
if (a[i] > 0) # If the value of a is greater than zero
a[i] = -a[i] # Converting value to negative value
k = 0
ii = i # Storing value of i in ii
print("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.length
k += 1
print(". ",ii,a,"\n")
end
i = ii # Storing the updated value of ii to i
addneg = 1
else # If the value of a is not greater than zero
if addneg == 1 # If the value of addneg is 1
j = i
addneg = 0 # Change the value of addneg to 0
else
if (j == i) # If i is equal to j
allneg = 1
end
end
i = (i + 1) % a.length # Update value of i
end
end
print(" *** DONE *** ")

Calculate the product of two matrices

Write a program to display the product of these two matrices a[3×4]a[3\times4] and b[4×2]b[4\times2], resulting in a matrix c[3×2]c[3\times2]:

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]]
Press + to interact
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,4
bROWS,bCOLS = 4,2
cROWS,cCOLS = 3,2
c = [[0,0],[0,0],[0,0]]
for i in 0...aROWS
for j in 0...aCOLS
for k in 0...bCOLS
c[i][k] += a[i][j] * b[j][k]
end
end
end
print("Matrix Product:\n",c)

In the program above:

  • We create two matrices, a and b.
  • We create variables (aROWS, aCOLS, bROWS, bCOLS, cROWS, and cCOLS) and assign them values (3, 4, 4, 2, 3, and 2 ,respectively).
  • We create a matrix, c[3][2], initialized with zeros.
  • We calculate the product of matrices a and b and store the result in c using three nested loops.
  • We display the resultant matrix, c, directly (as a multi-dimensional array) without using loops.