My First Geeky Post

Earlier this afternoon, while sitting in one of those lecture rooms with my mind not really on the lecture for some reasons, I thought of doing a post about a very simple Prime Number Generator program, which was originally made by me, including the algorithm used. Visual Basic was my program of choice.
Because I was bored in one of the lectures that I was attending, I formulated my own algorithm, just to keep my eyes wide open. Because, I don’t understand or refuse to understand the complex way of determining a prime number (like the Sieve of Eratosthenes and Bit Array Improvement),
I used the definition of a prime number as my basis for my algorithm. A prime number is a natural number which has exactly two distinct natural number divisors — one and itself. From there, I hit it. 
I thought of having just one input and this is the number of prime numbers (H) that you would want to display. Pressing the pushbutton would show the H prime numbers in a textbox. What I want my program to do is divide all natural numbers starting from 2 by 1 to that natural number (Num) and record the number of cases where the remainder yielded to zero (Count). This is achieved through the modulo command. Now, if the number of cases where the remainder is zero (Count) would be equal to two, then that natural number is prime. By the way, the divisors are known as d. Also, it traces the number of printed prime numbers (i) so that it will stop if the number of printed prime numbers is equal to the desired number of prime numbers that the user would want to display (H). By the way, I started with the least known prime number, 2. Starting from 1 would be unnecessary, aside from making the program more complex.
In a mathematical approach and through a concrete example, here is what I want to say:
Number of prime numbers to display: 3              *This is your H.

The program would do this calculations:
2/1 = 2 rem 0                      *count = 1
                                     2/2 = 1 rem 0                      *count = 2    ^2 is PRIME^     i = 1
3/1 = 3 rem 0                      *count = 1
3/2 = 1 rem 0.5                   *count = 0 
                                      3/3 = 1 rem 0                      *count = 2   ^3 is PRIME^      i = 2
4/1 = 4 rem 0                      *count = 1
4/2 = 2 rem 0                      *count = 2
4/3 = 1 rem 0.33                 *count = 2
                                      4/4 = 1 rem 0                      *count = 3  ^4 is not PRIME^  i = 2
5/1 = 5 rem 0                      *count = 1
5/2 = 2 rem 0.5                   *count = 1
5/3 = 1 rem 0.67                 *count = 1
5/4 = 1 rem 0.25                 *count = 1
                                     5/5 = 1 rem 0                      *count = 2  ^5 is PRIME^      i = 3
Loop stops since i = 3 and 3 is the number of prime number that you would want to display. Below is the actual code that I have used.
Private Sub CommandButton1_Click()

H = Val(TextBox1)

Num = 2

d = 1

i = 0

Count = 0

Do

        Do

            If Num Mod d <> 0 Then

                d = d + 1

            Else

                Count = Count + 1

                d = d + 1

            End If

        Loop Until d = Num + 1

          

If Count = 2 Then

 i = i + 1

 TextBox2.Text = TextBox2.Text + Str(Num)

 Count = 0

 d = 1

 Num = Num + 1

Else

 Num = Num + 1

 Count = 0

 d = 1

End If

Loop Until i = H

End Sub

Private Sub Label4_Click()

‘Code by MissChievous of http://orangepeelparaboloid.blogspot.com/

End Sub

Below is a snapshot of the program.

There. I know it’s simple(haha), but I found self-fulfillment in doing it that I want to share this with you, guys. Feel free to comment if you have any clarifications.

534 Views