The for loop, prime numbers

sopek

New member
Local time
Today, 02:13
Joined
Feb 6, 2016
Messages
3
The while loop, prime numbers

Hello.
I do not know how to finish this code is on display prime number after entering the initial and final value. Attached is a photo of my form.Please help.

Code:
Dim x, k, kk, i As Integer
    x = txt1
    k = txt2
    
    If Not IsNumeric(x) Then
    MsgBox "Non-numeric value", vbOKOnly, "Error"
    Exit Sub
    End If
    
    If Not IsNumeric(k) Then
    MsgBox "Non-numeric value", vbOKOnly, "Error"
    Exit Sub
    End If
    
    
    If x > k Then
    kk = x
    x = k
    k = kk
    
    ElseIf x = k Then
    MsgBox "Too small range", vbOKOnly, "Error"
    Exit Sub
    End If
    
    While k > x
    x = k
 

Attachments

  • Formularz.png
    Formularz.png
    7.2 KB · Views: 74
Last edited:
You have a few problems. You should really indent your code so that its readable like shown below.


Code:
Dim x As Integer
Dim k As Integer
Dim kk As Integer
Dim i As Integer


x = txt1
k = txt2

If Not IsNumeric(x) Then
    MsgBox "Non-numeric value", vbOKOnly, "Error"
    Exit Sub
End If

If Not IsNumeric(k) Then
    MsgBox "Non-numeric value", vbOKOnly, "Error"
    Exit Sub
End If

If x > k Then
    kk = x
    x = k
    k = kk
ElseIf x = k Then
    MsgBox "Too small range", vbOKOnly, "Error"
    Exit Sub
End If

Do While k > x
    'put code here
    MsgBox x
    
    x = x + 1
Loop

You can't declare your variables like you did and end up with integers. The way you have it only i ends up as an integer. I suggest putting them on separate lines. See http://www.cpearson.com/excel/declaringvariables.aspx for more on this.

The while loop needs some help. You need a Do and the beginning and a Loop at the end and most importantly something in the loop to make sure it ends for example the line x = x + 1. I suggest you use for next loop as you'll be less likely to end up with an infinite loop. See http://www.techonthenet.com/access/functions/advanced/for_next.php

I just put a Msgbox in the loop so there was something there. If you want to display the prime number in the range you will need to test them. https://en.wikipedia.org/wiki/Primality_test has an algorithm that does that. Its in Pseudocode but should be fairly easy to convert to VBA.
 
Thanks for the advice. Numerical interval is entered from the keyboard . I'm not sure what I'm there in the post where you wrote me to put the code.The code should look like this ?

Code:
Dim x As Integer
Dim k As Integer
Dim kk As Integer
Dim i As Integer


x = txt1
k = txt2

If Not IsNumeric(x) Then
    MsgBox "Wartość nienumeryczna !!!", vbOKOnly, "Error"
    Exit Sub
End If

If Not IsNumeric(k) Then
    MsgBox "Wartość nienumeryczna !!!", vbOKOnly, "Error"
    Exit Sub
End If

If x > k Then
    kk = x
    x = k
    k = kk
ElseIf x = k Then
    MsgBox "Za mały zakres !!!", vbOKOnly, "Error"
    Exit Sub
End If

Do While k > x

 For i = x To k
    If i Mod 2 = 0 Then
    x = x + 1
    txt3 = txt3 & " " & i
    End If
    Next i
    
Loop
 
Last edited:
Code:
 if X Mod 2 = 0 Then x = x + 1
    
 for i = x to k step 2
    txt3 = txt3 & " " & X
 Next i
 
The code should look like this ?

Code:
Dim x As Integer
Dim k As Integer
Dim kk As Integer
Dim i As Integer


x = txt1
k = txt2

If Not IsNumeric(x) Then
    MsgBox "Wartość nienumeryczna !!!", vbOKOnly, "Error"
    Exit Sub
End If

If Not IsNumeric(k) Then
    MsgBox "Wartość nienumeryczna !!!", vbOKOnly, "Error"
    Exit Sub
End If

If x > k Then
    kk = x
    x = k
    k = kk
ElseIf x = k Then
    MsgBox "Za mały zakres !!!", vbOKOnly, "Error"
    Exit Sub
End If

Do While k > x

 For i = x To k
    If x Mod 2 = 0 Then x = x + 1
    txt3 = txt3 & " " & x
    End If
    Next i
Loop
 
Definitely not. The code

Code:
 if X Mod 2 = 0 Then x = x + 1
    
 for i = x to k step 2
    txt3 = txt3 & " " & X
 Next i

produces copies of x and even if it were

Code:
 if X Mod 2 = 0 Then x = x + 1
    
 for i = x to k step 2
    txt3 = txt3 & " " & i
 Next i

It would produce odd numbers, not prime numbers.

You don't want to need two loops at least not if you just test each number for primality. I suggest rewriting the following function from https://en.wikipedia.org/wiki/Primality_test in VBA

function is_prime(n : integer)
if n ≤ 1
return false
else if n ≤ 3
return true
else if n mod 2 = 0 or n mod 3 = 0
return false
let i ← 5
while i×i ≤ n
if n mod i = 0 or n mod (i + 2) = 0
return false
i ← i + 6
return true

And add that function to your module and then call the function in your code like

Code:
Dim x As Integer
Dim k As Integer
Dim kk As Integer
Dim i As Integer


x = txt1
k = txt2

If Not IsNumeric(x) Then
    MsgBox "Wartość nienumeryczna !!!", vbOKOnly, "Error"
    Exit Sub
End If

If Not IsNumeric(k) Then
    MsgBox "Wartość nienumeryczna !!!", vbOKOnly, "Error"
    Exit Sub
End If

If x > k Then
    kk = x
    x = k
    k = kk
ElseIf x = k Then
    MsgBox "Za mały zakres !!!", vbOKOnly, "Error"
    Exit Sub
End If

For i = x To k
    If is_prime(i) Then 
          txt3 = txt3 & " " & i
    End If
Next i
 
On second thought it might be a better learning experience for you if you wrote your own simple function to determine primality. I suggest doing it brute force. Just check the number to see if it is divisible by any number lower than itself or one.
 
Code:
Public Function isPrime(n As Long) As Boolean
    Dim i As Long, w As Long
    If n = 2 Then isPrime = True: Exit Function
    If n = 3 Then isPrime = True: Exit Function
    If n Mod 2 = 0 Then isPrime = False: Exit Function
    If n Mod 3 = 0 Then isPrime = False: Exit Function

    i = 5
    w = 2

    While i * i <= n
        If n Mod i = 0 Then isPrime = False: Exit Function

        i = i + w
        w = 6 - w
    Wend

    isPrime = True
End Function
 

Users who are viewing this thread

Back
Top Bottom