Dear Access Forum Experts,
I have numbers starting 1 to 500,000,000,000 and would like to check/segregate them whether it’s Prime or Composite. So appreciate if somebody could provide me a solution.
thanks
Dear Access Forum Experts,
I have numbers starting 1 to 500,000,000,000 and would like to check/segregate them whether it’s Prime or Composite. So appreciate if somebody could provide me a solution.
thanks
Is this an assignment?
I ask because finding Primes is a classic College assignment and you wouldn't be wanting us to do your work for you would you... ?
DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
Please use the star below the post to say thanks if we have helped !
↓↓ It's down here ↓↓
if it was an assignment, I would have easily solve with the following formula in excel (=IF(J4=2,"Prime",IF(AND(MOD(J4,ROW(INDIRECT("2:"& ROUNDUP(SQRT(J4),0))))<>0),"Prime","Composite"))
The reason I requested through AccessForum is that Excel is straggling to handle huge data.... and maybe there is an option in Access to segregate numbers (as mentioned below) faster without time consuming
Table:
Inventory Prime/Composite 41022242542 Composite 41022242543 Composite 41022242544 Composite 41022242545 Composite
Last edited by Gulya; 01-20-2021 at 07:18 AM. Reason: ammend
Just checking
I doubt if access will be much more efficient to be honest
This function will return a result and test it for you
It took 15 seconds to run the loop and display 10000 records.Code:Public Function fnIsPrime(lInput As Long) As Boolean Dim i As Long fnIsPrime = True If lInput = 1 Then fnIsPrime = False Exit Function End If For i = 2 To lInput - 1 If lInput Mod i = 0 Then fnIsPrime = False Exit Function End If Next i End Function Sub testPrime() Dim lNumber As Long Dim sAns As Boolean Dim tTime As Date tTime = Now For lNumber = 1 To 10000 sAns = fnIsPrime(lNumber) Debug.Print lNumber; sAns Next lNumber Debug.Print tTime; Now End Sub
You could probably speed it up by not checking even numbers, as they can't be a prime?
DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
Please use the star below the post to say thanks if we have helped !
↓↓ It's down here ↓↓
The first test is division by 2 so it does that?You could probably speed it up by not checking even numbers, as they can't be a prime?
For i = 2 To lInput - 1
If lInput Mod i = 0 Then
EDIT - I guess you meant bypass them altogether, but the function call would have to test that. However, the code already tests that so I think you'd just be sort of replacing one test with another - except the current one is going to run anyway?
The more we hear silence, the more we begin to think about our value in this universe.
Paraphrase of Professor Brian Cox.
@Micron - Yes you are correct, it's a generic function.
What I meant to say and didn't put very eloquently was - don't pass them into the function in the first place.
e.g assuming you have a list of all your input numbers query it to only include the odd ones.
Even now I'm making a pigs ear of describing that...
DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
Please use the star below the post to say thanks if we have helped !
↓↓ It's down here ↓↓
I edited my post before I saw yours. I think what you have there is efficient enough. First, you'd have to figure out if a number is a prime and if so, remove it from your list. That would be doing what your function is designed to do for you, thus you wouldn't need the function? Without using the function, are you going to remove 421589 from that list? The answer is yes, by the way. If you did remove them all from your list, then you'd have only primes left.
The more we hear silence, the more we begin to think about our value in this universe.
Paraphrase of Professor Brian Cox.
I just changed the test routine to this and the time to execute went down to 2 seconds!
Improvements : only test the odd numbers and only print out the primes. The debug always has an overhead.
Code:Sub testPrime() Dim lNumber As Long Dim sAns As Boolean Dim tTime As Date tTime = Now For lNumber = 1 To 10000 Step 2 sAns = fnIsPrime(lNumber) If sAns = "true" Then Debug.Print lNumber; sAns Next lNumber Debug.Print Now; tTime End Sub
DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
Please use the star below the post to say thanks if we have helped !
↓↓ It's down here ↓↓
Thanks a lot Minty for the response, could you please share the template as i'm straggling to use the provided codes plzI just changed the test routine to this and the time to execute went down to 2 seconds!
Improvements : only test the odd numbers and only print out the primes. The debug always has an overhead.
Code:Sub testPrime() Dim lNumber As Long Dim sAns As Boolean Dim tTime As Date tTime = Now For lNumber = 1 To 10000 Step 2 sAns = fnIsPrime(lNumber) If sAns = "true" Then Debug.Print lNumber; sAns Next lNumber Debug.Print Now; tTime End Sub
There is no template just the code, and you will need to adjust the data types to LongLong to handle that size of number.
Also how did you expect to handle that volume of rows in excel?
At a million rows per spreadsheet you will need 500,000 spreadsheets (That is half a million! ) to handle the output from the routine.
Even running that on a healthy SQL server would take ages to generate the results and how would you store them, certainly not in Access.
So perhaps having piqued my interest you could explain what the real purpose of this question is? As what you are asking for and your premise for asking are still unclear.
By the way while testing I found 4,294,967,291 is a prime.
DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
Please use the star below the post to say thanks if we have helped !
↓↓ It's down here ↓↓
DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
Please use the star below the post to say thanks if we have helped !
↓↓ It's down here ↓↓