Created
June 22, 2022 05:59
-
-
Save dasbluehole/fe33de2587b2baa2230f64b28b08bb0a to your computer and use it in GitHub Desktop.
Gambas3 Primes routines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' Gambas module file | |
Public Sub Main() | |
Dim l_bound, u_bound As Integer | |
Print "Primes between a range [Note: 1 is special and 2 is even Prime]" | |
Print "Input lower bound (should be greater than 2)" | |
Input l_bound | |
Print "Input upper bound ( should be greter than lower bound) "; l_bound | |
Input u_bound | |
Print "" | |
Print "Lower bound = "; l_bound; " Upper bound = "; u_bound | |
primes_between(l_bound, u_bound) | |
Dim x As Integer | |
Dim a As Array | |
a = prime_array_between(l_bound, u_bound) | |
For x = 0 To a.Length - 1 | |
Print a[x] | |
Next | |
' first 10 primes | |
a = prime_array(10) | |
For x = 0 To a.Length - 1 | |
Print a[x] | |
Next | |
End | |
' checks if a number is prime | |
' input positive integer | |
' returns true if the input is prime else false | |
Public Function is_prime(x As Integer) As Boolean | |
Dim i As Integer | |
For i = 2 To CInt(Ceil(Sqr(x))) | |
'Print "--- checking with "; i, x; "%"; i; " = "; x % i | |
If x % i = 0 Then | |
Return False | |
Endif | |
Next | |
Return True | |
End | |
' make a prime array | |
' this function creates a dynamic array of integer stores desired numbers of primes | |
' in it and returns the array | |
' input number of primes desired | |
' output returns array containing desired number of primes | |
Public Function prime_array(n As Integer) As Integer[] | |
Dim i, j As Integer | |
i = 1 | |
j = 3 | |
Dim arr As New Integer[] | |
arr.Add(2) | |
While i < n | |
If is_prime(j) Then | |
arr.Add(j) | |
i += 1 | |
Endif | |
j += 1 | |
Wend | |
Return arr | |
End | |
' prints prime numbers between 2 numbers | |
' input lower bound , upper bound | |
' output prints all primes in the inclussive range | |
Public Sub primes_between(lb As Integer, ub As Integer) | |
If lb < 3 Then | |
Print 2 | |
lb = 3 | |
Endif | |
If ub < lb Then | |
ub = lb | |
Endif | |
While lb <= ub | |
If is_prime(lb) Then | |
Print lb | |
Endif | |
lb += 1 | |
Wend | |
End | |
' make a prime array | |
' this function creates a dynamic array of integer stores primes between range | |
' in it and returns the array | |
' input lower bound and upper bound | |
' output returns array containing primes inclusive of bounds | |
Public Function prime_array_between(lb As Integer, ub As Integer) As Integer[] | |
Dim arr As New Integer[] | |
If lb < 3 Then | |
arr.Add(2) | |
lb = 3 | |
Endif | |
If ub < lb Then | |
ub = lb | |
Endif | |
While lb <= ub | |
If is_prime(lb) Then | |
arr.Add(lb) | |
Endif | |
lb += 1 | |
Wend | |
Return arr | |
End |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment