Randomness of Prime Numbers | Maths Explanation for VB.Net Kids
Prime numbers are natural numbers greater than 1 with no positive divisors other than 1 and itself. Prime numbers, in ascending order, are:
Because prime numbers are so random in their progression,
they are very difficult to predict as they get larger.
For the reason of their unpredictability, prime number are applied in
- Cryptography: RSA encryption relies on large prime numbers.
- Hashing: Prime numbers help reduce collisions in hash functions.
- Data structures: Primes are used in sizing hash tables and optimizing algorithms.
In this beginner-friendly Maths Visual Basic tutorial for kids, we'll show how to list prime numbers in a fun and interactive way for STEM students.
Writing a Visual Basic (VB.Net) code to list prime numbers will involve checking every number in a range of interest and gathering those that are without factors.
Logic for Determining Prime Numbers | Detailed Explanation for VB.Net Kids
Say we want to implement a VB.Net algorithm to list all prime numbers between 2 and 100 inclusive,
we would start from 2; check to see whether it has any factors;
keep it as a prime number if it has no factors; otherwise discard it.
We would then go to 3, and repeat the same process.
Repeat same for 4, 5, 6, 7, ..., 98, 99, 100.
But we can always use a few tricks for the VB.Net algorithm...
Step 1:
First, we'll start our range from 9 (keeping 2, 3, 5, and 7 as prime numbers).
Step 2:
Next, we'll only check through odd numbers.
Step 3:
Next, we'll check against the factors 2, 3, and 5.
Step 4:
Lastly, we'll check with a subset of smaller
prime numbers that we'll gather as our check progresses.
Create a new VB.Net class file; Project, Add Class.
Call it listPrimeNumbers.vb.
Optionally, Create a new VB.Net module file; Project, Add Module.
Call it ListPrimeNumbersModule.vb.
Type out the adjoining VB.Net code for listing prime numbers.
Note: You can reuse the main VB.Net module from the previous lesson if you have been following.
So! Visual Basic Fun Practice Exercise - List Prime Numbers
As a fun practice exercise, feel free to try out your own boundary values, and see how the Visual Basic code lists the prime numbers between those boundary values.
VB.Net Code for List Prime Numbers - Class File.
Dim first As Integer
Dim last As Integer
Dim progress As Integer
Dim list_of_primes As New List(Of Integer)(New Integer() {2, 3, 5, 7})
Dim index As Integer
Dim square_root As Double
Dim do_next_iteration As Boolean
Dim result As New List(Of Integer)
' Simulate a constructor
Public Sub _init_(alpha As Integer, omega As Integer)
first = alpha
last = omega
' STEP 1:
progress = 9
square_root = 0
End Sub
' Returns a list Of the desired Set Of prime numbers
Public Function getPrimes() As List(Of Integer)
' STEP 2:
For progress = progress To last Step 2
do_next_iteration = False ' a flag
' STEPS 3, 4:
' Check through already accumulated prime numbers
' to see if any Is a factor of "progress".
square_root = Val(Math.Ceiling(Math.Sqrt(progress)))
index = 0
Do While list_of_primes.Item(index) <= square_root
If progress Mod list_of_primes.Item(index) = 0 Then
do_next_iteration = True
Exit Do
End If
index += 1
Loop
If do_next_iteration = True Then
Continue For
End If
' if all checks are scaled,then "progress" Is our guy.
list_of_primes.Add(progress)
Next progress
Return list_of_primes
End Function
End Class
VB.Net Code for List Prime Numbers - Main Module.
Sub Main()
' Use the even number Class
Dim lower_boundary = 1
Dim upper_boundary = 100
Dim prime_list As New PrimeNumbers
prime_list._init_(lower_boundary, upper_boundary)
Dim answer = prime_list.getPrimes()
Console.WriteLine("Prime numbers between " & lower_boundary & " and " & upper_boundary & " are:")
Console.WriteLine(String.Join(", ", answer))
End Sub
End Module