Numbers trouble

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm trying to print certain numbers between 1 and 100 and when I run it, it
prints all 100 numbers.
 
?From a table? ?In a query? ?Using a procedure? ?Via a report?

Are you using MS Access?

Why? (as in "what will you do with the numbers once printed?")

Have you looked at using Excel?

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
Sorry, I wasn't very specific.
Its in Microsoft Access, in Modules.
I'm just starting to use VBA and I'm stuck, once again, and I need a little
help.
 
You helped me out with it before (I don't need the answer I just need a hint)

Public Function Prime100(y As Long) As Variant
Dim x As Single

For Prime100 = 1 To 100
If y = 1 Or y < 0 Then
Exit Function
Debug.Print (1 Or y < 0)
End If

For x = 2 To (y - 1)
If y / x = Int(y / x) Then
Exit Function
End If
Next x

Debug.Print "prime100 = " & Prime100

Next

End Function
 
what happens is, when I write "? Prime100(number)" The "number has to be a
prime number, otherwise nothing shows up, and when I type in a prime number
the numbers 1 to 100 show.
 
The problem is in here:

For x = 2 To (y - 1)
If y / x = Int(y / x) Then
Exit Function
End If
Next x

You're exiting the function when you find a number that can divide into y.
You don't want that: you simply want to stop the loop.

Is your attempt to print the first 100 primes? There's no reason to pass y
to the function, and your loop

For Prime100 = 1 To 100

isn't really appropriate.

What you want is a loop of odd numbers starting at 3, and check whether each
number is prime or not.

If it's prime, print it out and increment the number found by one.

Once your number found hits 100, end the routine.
 
Ok, I understood that, except, what did you mean by "There's no reason to
pass y to the function, and your loop"?
 
That was two separate statements.

1. There's no reason to pass y to the function
2. Your loop

For Prime100 = 1 To 100

isn't really appropriate.
 
Ok, this is what I have now:
Option Compare Database
Option Explicit

Public Function Prime100(y As Long) As Variant
Dim x As Single

For Prime100 = 3 To 100
If Prime100 = Not ((y Mod 2) = 0) Then
y = 1 Or y < 0

End If

For x = 2 To (y - 1)
If y Mod x = Int(y Mod x) Then

End If
Next x

Debug.Print "prime100 = " & Prime100

Next Prime100

End Function

But I'm still getting the same thing, I'm not even getting odd numbers
 
Go back to the original function you had that checked whether a particular
number was prime or not. (I don't remember what it was called: I'm using
IsPrime below)

Use something like:

Sub Prime100()
Dim lngCount As Long
Dim lngValue As Long

lngValue = 2
lngCount = 1
Debug.Print lngValue
lngValue = 3
Do While lngCount < 100
If IsPrime(lngValue) Then
Debug.Print lngValue
lngCount = lngCount + 1
End If
lngValue = lngValue + 2
Loop

That will print the first 100 primes to the Immediate window (Ctrl-G)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top