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

            If Num Mod d <> 0 Then
                d = d + 1
                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
 Num = Num + 1
 Count = 0
 d = 1
End If
Loop Until i = H

End Sub

Private Sub Label4_Click()
'Code by MissChievous of
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.


  1. ahhh. ano daw??? hahaha. i love numbers Miss C because i'm an accountant but not this kind, hehehe. i prefer the ones with a "$" sign in front but thanks for sharing.

  2. Lol. It's a program, Gee. Though I am an engineer I love numbers with '$', too. Thanks for commenting! I changed my comment form. A friend suggested on doing this.

  3. Nice work :-)

    There are a couple of changes that would make your program run even faster:

    Start by testing division by 2 instead of 1. Everything divides by 1.

    As above, plus stop counting zero reminders after you've found 2. By then you already know it's not prime.

    Stop testing division at sqrt(number) instead of number. Stop counting remainders after you've found 1.

    Sorry, I couldn't help myself :-P

  4. I think 1 is a prime number. I would say it is a good method just to count the number of times it can be divided with rem 0. One improvement is that if count = 3, straight away end the loop and go to the next number as there is no need to count anymore (unnecessary).


Post a Comment

Popular posts from this blog

Post Board

Tony and Jackey Review | L'Oreal Digital Perm + Korean Hair Color

Five Affordable Places to Go in Manila This Valentines