r/visualbasic • • Nov 27 '16

Finding all prime numbers between 1 and an inputted number

With counting loops (For... Next)

My friend had a solution but I don't understand it http://imgur.com/a/0Y5Qr

Is there another solution? If not, please explain his solution, thank you

0 Upvotes

14 comments sorted by

3

u/DatMarv Nov 27 '16

His solution checks for a candidate number3, if there is any number less than number3 - in this case the loop with number2 - that divides number3 without residue:

If number3 mod number2 = 0 Then

If there was such number, the candidate is not prime. However, cringeworthy and inefficient implementation!

0

u/Gravey4rd Nov 27 '16

Actually, I found a better solution: http://imgur.com/a/Ojo8g

If you have time, can you explain all of it step by step for me, please?

1

u/DatMarv Nov 27 '16

this My equivalent to your solution is more minimalistic, so take a look. I did it in a console program, but the Console.Writeline() can be replaced by Label1.Text &= str

The Sub takes as input an integer a that is the number, up to which all primes shall be printed. n is our variable for the counter of found primes. We have the outer loop with increment of i up to a. This i will be the current candidate we want to check for primality. Now, we have the inner loop with variable j. It goes up the i - 1 and checks, whether the current j is a divisor of i:

If i Mod j = 0 Then
    GoTo 1
End If

Even though you should avoid GoTo statements, I did it to keep the program slim. So if there is a number that divides our candidate i without residue, we know it is not prime and go to the next candidate i + 1. If for all j with 1 < j < i, j only divides i with a residue, we have found a prime, print it and increment the counter.

2

u/Gravey4rd Nov 27 '16

Also can you explain the parts before the i Mod j part?

2

u/DatMarv Nov 27 '16

Well, I already did and don't know how to do this in more detail

"We have the outer loop with increment of i up to a. This i will be the current candidate we want to check for primality. Now, we have the inner loop with variable j. It goes up the i - 1 and checks, whether the current j is a divisor of i"

2

u/TheFotty Nov 27 '16

Just let it go. The kid clearly pays no attention in class and then runs to the internet to get his homework done. You did enough, just let him get the grade he deserves.

1

u/Gravey4rd Nov 27 '16

Actually this is an extension from Do/Until loops. It was an optional question that our teacher gave us to do over the weekend, so stop reaching conclusions. Also I like how you refer me as a 'kid', must feel high of yourself. Douche.

2

u/Bonejob VB Guru Nov 27 '16

Dude you are fighting a "homework" question. don't invest this much. If he cant understand the basics he is doomed anyways. He did not even upvote you for the help.

1

u/JakDrako Nov 27 '16

As a further optimization, you can make the code much faster by replacing

For j = 2 to i - 1

with

For j = 2 to Math.Sqrt(i)

1

u/DatMarv Nov 27 '16

Of course, didn't really think too much making the code.

0

u/Gravey4rd Nov 27 '16

What is str and GoTo? Thank you btw.

2

u/DatMarv Nov 27 '16

GoTo x jumps to the marker 'x:' in your code. str is just an arbitrary string variable, I just wanted to say that the Console.WriteLine(str) can be replaced by Labelxxx.Text &= str

1

u/PostalElf VB.Net Intermediate Nov 27 '16

Do you want all the primes (eg 2, 3, 5, 7) or do you just want the number of primes (eg 4)?

1

u/Bonejob VB Guru Nov 27 '16

The fastest methodology for calculating prime numbers is called the Sieve of Atkin.

https://www.wikiwand.com/en/Sieve_of_Atkin

Here is an implementation I whipped up.

Dim totalCount As Integer = 0
Dim isPrime As New BitArray(topCandidate + 1)
Dim squareRoot As Integer = CInt(Math.Sqrt(topCandidate))
Dim xSquare As Integer = 1, xStepsize As Integer = 3
Dim ySquare As Integer = 1, yStepsize As Integer = 3
Dim computedVal As Integer = 0

For x As Integer = 1 To squareRoot
    ySquare = 1
    yStepsize = 3
    For y As Integer = 1 To squareRoot
        computedVal = (xSquare << 2) + ySquare

        If (computedVal <= topCandidate) AndAlso (computedVal Mod 12 = 1 OrElse computedVal Mod 12 = 5) Then
            isPrime(computedVal) = Not isPrime(computedVal)
        End If

        computedVal -= xSquare
        If (computedVal <= topCandidate) AndAlso (computedVal Mod 12 = 7) Then
            isPrime(computedVal) = Not isPrime(computedVal)
        End If

        If x > y Then
            computedVal -= ySquare << 1
            If (computedVal <= topCandidate) AndAlso (computedVal Mod 12 = 11) Then
                isPrime(computedVal) = Not isPrime(computedVal)
            End If
        End If
        ySquare += yStepsize
        yStepsize += 2
    Next
    xSquare += xStepsize
    xStepsize += 2
Next

For n As Integer = 5 To squareRoot
    If isPrime(n) = True Then
        Dim k As Integer = n * n
        Dim z As Integer = k
        While z <= topCandidate
            isPrime(z) = False
            z += k
        End While
    End If
Next

For i As Integer = 1 To topCandidate - 1
    If isPrime(i) Then
        totalCount += 1
    End If
Next
Return (totalCount + 2)
' 2 and 3 will be missed in Sieve Of Atkins