Results 1 to 5 of 5

Thread: Getting the nearest prime number

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    78

    Getting the nearest prime number

    Can someone give a simple code on how to get the nearest prime number?
    For example:
    Key Value: 0...60
    Prime Number: 59 (since 59 is the nearest prime number to 60)

    Note:
    Prime number < key value
    To become a PROFESSIONAL,
    Start from SCRATCH...

  2. #2
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: Getting the nearest prime number

    What values might you be dealing with? There are multiple ways of doing this, but the best one will depend on your needs.. Would it be feasible to make a list of the primes in range before beginning? That would drastically speed things up.. Does it need to be the absolute closest prime? Because there are certain sets of primes that can be found relatively easily.. Beyond that, there are beowulf clusters dedicated to calculating large primes.. Its really a cpu intensive process regardless of method..

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    78

    Re: Getting the nearest prime number

    I just need the closest prime number, meaning the largest among the prime numbers that is less than the key value.
    To become a PROFESSIONAL,
    Start from SCRATCH...

  4. #4
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: Getting the nearest prime number

    I know it ain't code but you should be able to distill it starting at this page
    http://en.wikipedia.org/wiki/Primality_test
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    78

    Re: Getting the nearest prime number

    Problem Solved!!!
    I just used this code:

    PrimeNumber = 0
    For w = 1 To Val(txtFileSize.Text)
    PrimeNumSwitch = 1
    For z = 9 To 2 Step -1
    If w = z Then
    PrimeNumSwitch = 1
    ElseIf w Mod z <= 0 Then
    PrimeNumSwitch = 0
    End If
    Next

    If PrimeNumSwitch = 1 Then
    If w > PrimeNumber Then
    PrimeNumber = Val(txtKeyValue(x).Text) Mod w
    End If
    End If
    Next

    lblAnswer(x).Caption = PrimeNumber

    Thanks for the replies!
    To become a PROFESSIONAL,
    Start from SCRATCH...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width