Results 1 to 12 of 12
  1. #1
    Gulya is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Jan 2021
    Posts
    7

    Prime or Composite

    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

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    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 ↓↓

  3. #3
    Gulya is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Jan 2021
    Posts
    7
    Quote Originally Posted by Minty View Post
    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... ?
    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

  4. #4
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    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

    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
    
    It took 15 seconds to run the loop and display 10000 records.

    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 ↓↓

  5. #5
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,791
    You could probably speed it up by not checking even numbers, as they can't be a prime?
    The first test is division by 2 so it does that?

    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.

  6. #6
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Quote Originally Posted by Micron View Post
    The first test is division by 2 so it does that?

    For i = 2 To lInput - 1
    If lInput Mod i = 0 Then
    @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 ↓↓

  7. #7
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,791
    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.

  8. #8
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    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 ↓↓

  9. #9
    Gulya is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Jan 2021
    Posts
    7
    Quote Originally Posted by Minty View Post
    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
    Thanks a lot Minty for the response, could you please share the template as i'm straggling to use the provided codes plz

  10. #10
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    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 ↓↓

  11. #11
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,791
    Quote Originally Posted by Minty View Post
    By the way while testing I found 4,294,967,291 is a prime.
    What is the significance of that number?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  12. #12
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Quote Originally Posted by Micron View Post
    What is the significance of that number?
    Absolutely nothing that I am aware of. It was just a very big number and is a prime.

    Unless of course...

    You are a paid up member of the tin hat brigade
    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 ↓↓

Please reply to this thread with any new information or opinions.

Similar Threads

  1. AutoNumber vs. Composite Key
    By sra2786 in forum Database Design
    Replies: 2
    Last Post: 12-10-2018, 04:32 PM
  2. How to create a unique composite value
    By PackerIntl in forum Programming
    Replies: 13
    Last Post: 03-31-2014, 12:59 PM
  3. Composite key or autonumber PK
    By Liam87 in forum Access
    Replies: 4
    Last Post: 11-06-2012, 07:39 PM
  4. composite key question
    By revnice in forum Access
    Replies: 2
    Last Post: 08-08-2010, 12:27 PM
  5. composite unique key
    By shital in forum Access
    Replies: 1
    Last Post: 07-09-2010, 08:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums