none
Stern Prime RRS feed

  • Question

  • How can I get the 'p' and 'q' if I input 9? should be only ODD integer..

    9 = 7 + 2 x 1 ^ 2 --->> 9 = p + 2 x q ^ 2

    15 = 7 + 2 x 2 ^ 2

    21 = 3 + 2 x 3 ^ 2

    25 = 7 + 2 x 3 ^ 2

    27 = 19 + 2 x 2 ^ 2

    x = 1 For x = 0 To b x += 1 b = x m = 2 * x ^ 2 p = q - m End If

    textbox1.text = txtinput.text & p & " + 2 x " & q & " ^2" Next


    This tried everything I could but ..


    • Edited by codecodex Tuesday, January 29, 2013 1:58 PM
    Tuesday, January 29, 2013 1:45 PM

Answers

  • Every odd integer inputted, it says "If (odd)  is prime, it is a Stern Prime". And all accepted inputs are odd integers only no even nos.

    I'll just rearrange the codes you gave and try to get the output I need. I really appreciate your help! It's great, Thanks a lot! :)) 

    I'm sorry, I made a mistake in the test after calling NotSternParts. Here is the corrected code. I also changed it to allow the number to be entered in a TextBox.

    Sub Button2_Click(sender As Object, e As EventArgs)
        Dim checkNumber As Long 
        If Not Long.TryParse(TextBox1.Text, checkNumber) Then
            MessageBox.Show("Enter a valid integer")
            Exit Sub 
        End If
        Dim parts() As Long = NotSternParts(checkNumber)
        If parts(0) = 0 Then
            MessageBox.Show("If " & checkNumber.ToString & " is prime, it is a Stern Prime")
        Else
            MessageBox.Show(checkNumber.ToString & " cannot be a Stern Prime as it equals " _
              & parts(0).ToString & " + " & parts(1).ToString & " ^ 2")
        End If
    End Sub
    

    • Marked as answer by codecodex Tuesday, January 29, 2013 4:50 PM
    Tuesday, January 29, 2013 4:34 PM

All replies

  • Is this VB script, we see no types, so I assume it is. 

    Try a scripting forum either VBA or VBS 


    Success
    Cor

    Tuesday, January 29, 2013 2:11 PM
  • it's vs2010.
    Tuesday, January 29, 2013 2:21 PM
  • [Edit]

    Some code is missing in this post. Please see my later post for the full code.

    Here is some code that will find all the Stern Primes below 10,000 

    Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim sternPrimes As New List(Of Long)
        Dim q As Long = 2
        Do
            If IsStern(q) Then sternPrimes.Add(q)
            q = NextPrime(q)
        Loop While q < 10000
        MessageBox.Show("Found " & sternPrimes.Count.ToString & " Stern Primes")
    End Sub
        
    Public Shared Function IsStern(q As Long) As Boolean 
        ' A prime number q is a Stern Prime if it is not the sum of a small prime and twice the square of a non-zero integer
        ' In other words q <> p = 2 * b ^ 2 (where p and q are prime and b is a non-zero integer)
        Dim p As Long = 2
        Do While p < q
            Dim test As Long = 0
            Dim b As Long = 0
            Do While test < q
                b += 1
                test = p + 2 * b * b
                If test = q Then Return False
            Loop
            p = Primes.NextPrime(p)
        Loop
        Return True 
    End Function
        
    ''' <summary>Return the prime number that follows a given number</summary>
    ''' <param name="num">The number to start from (need not be prime)</param>
    ''' <returns>The next higher number that is prime</returns>
    Public Shared Function NextPrime(num As Long) As Long
        If num < 3 Then Return If(num < 2, 2, 3)
    
        Dim adj As Integer = If((num + 1) Mod 6 < 2, -1, 1)
        num = ((num + 1) \ 6) * 6 + adj 'Prime numbers > 3 must be 6k +/- 1
    
        Do
            num += 3 + adj
            adj = -adj
        Loop Until IsPrime(num)
    
        Return num
    End Function


    • Edited by Blackwood Tuesday, January 29, 2013 3:06 PM
    Tuesday, January 29, 2013 2:54 PM
  • In Function Istern: Primes won't work and In Function NextPrime: IsPrime 
    Tuesday, January 29, 2013 3:03 PM
  • In Function Istern: Primes won't work and In Function NextPrime: IsPrime 

    Sorry. I didn't copy all the code I should have.  This should be the full version.

    Sub Button2_Click(sender As Object, e As EventArgs)
        Dim sternPrimes As New List(Of Long)
        Dim q As Long = 2
        Do
            If IsStern(q) Then sternPrimes.Add(q)
            q = NextPrime(q)
        Loop While q < 10000
        MessageBox.Show("Found " & sternPrimes.Count.ToString & " Stern Primes")
    End Sub
        
    Public Shared Function IsStern(q As Long) As Boolean 
        ' A prime number q is a Stern Prime if it is not the sum of a small prime and twice the square of a non-zero integer
        ' In other words q <> p = 2 * b ^ 2 (where p and q are prime and b is a non-zero integer)
        Dim p As Long = 2
        Do While p < q
            Dim test As Long = 0
            Dim b As Long = 0
            Do While test < q
                b += 1
                test = p + 2 * b * b
                If test = q Then Return False
            Loop
            p = NextPrime(p)
        Loop
        Return True 
    End Function
        
    ''' <summary>Return the prime number that follows a given number</summary>
    ''' <param name="num">The number to start from (need not be prime)</param>
    ''' <returns>The next higher number that is prime</returns>
    Public Shared Function NextPrime(num As Long) As Long
        If num < 3 Then Return If(num < 2, 2, 3)
    
        Dim adj As Integer = If((num + 1) Mod 6 < 2, -1, 1)
        num = ((num + 1) \ 6) * 6 + adj 'Prime numbers > 3 must be 6k +/- 1
    
        Do
            num += 3 + adj
            adj = -adj
        Loop Until IsPrime(num)
    
        Return num
    End Function
        
    ''' <summary>Checks whether a given number is prime</summary>
    ''' <param name="num">The number to check (no exception is thrown for numbers less than 2)</param>
    ''' <returns>True if the number is prime</returns>
    Public Shared Function IsPrime(num As Long) As Boolean
        If num < 9 Then
            If num < 2 Then Return False
            If num < 4 Then Return True
            If num Mod 2 = 0 Then Return False
            Return True
        ElseIf num Mod 2 = 0 Then
            Return False
        End If
        If num Mod 3 = 0 Then Return False
    
        Dim maxFactor As Long = CLng(Math.Floor(Math.Sqrt(num)))
        Dim factor As Long = 5
        Do While factor <= maxFactor
            If num Mod factor = 0 Then Return False
            If num Mod (factor + 2) = 0 Then Return False
            factor += 6
        Loop
    
        Return True
    End Function
    

    Tuesday, January 29, 2013 3:09 PM
  • This is Great! Thanks ..

     But I need to input the odd integer then the output should be the stern prime. For example, I input 9 then the output should be 7 + 2 x 1 ^ 2.

    Tuesday, January 29, 2013 3:22 PM
  • This is Great! Thanks ..

     But I need to input the odd integer then the output should be the stern prime. For example, I input 9 then the output should be 7 + 2 x 1 ^ 2.

    I'm not sure why you would want to do that except as part of the process of finding a Stern Prime, but if that is what you want, try this code (you will still need the NextPrime and IsPrime functions from my earlier post).

    Sub Button2_Click(sender As Object, e As EventArgs)
        Dim checkNumber As Long = 9
        Dim parts() As Long = NotSternParts(checkNumber)
        If parts(0) > 0 Then
            MessageBox.Show("If " & checkNumber.ToString & " is prime, it is a Stern Prime")
        Else
            MessageBox.Show(checkNumber.ToString & " cannot be a Stern Prime as it equals " _
              & parts(0).ToString & " + 2 * " & parts(1).ToString & " ^ 2")
        End If
    End Sub
        
    Public Shared Function NotSternParts(q As Long) As Long()
        Dim p As Long = 2
        Do While p < q
            Dim test As Long = 0
            Dim b As Long = 0
            Do While test < q
                b += 1
                test = p + 2 * b * b
                If test = q Then Return {p, b}
            Loop
            p = NextPrime(p)
        Loop
        Return {0l, 0l} 'Note: zero followed by lower case "L"
    End Function



    • Edited by Blackwood Tuesday, January 29, 2013 4:20 PM
    Tuesday, January 29, 2013 3:41 PM
  • How about the output?

    Tuesday, January 29, 2013 3:54 PM
  • If you are trying to calculate all Stern Primes :

            Dim SternPrimes As New List(Of Long)
            SternPrimes.AddRange({2, 3, 17, 137, 227, 977, 1187, 1493})
            'The next Stern Prime, if it exists, is larger than 2*10^13. - Benjamin Chaffin, Mar 28 2008
            'http://oeis.org/A042978
    

    Tuesday, January 29, 2013 3:58 PM
  • How about the output?

    The two numbers you asked for (p and b: 7 and 1 if q=9) are in the array returned by NotSternParts. I don't know what you want to do with them, but as an example, I put them in the MessageBox. If the number is 9, it says "9 cannot be a Stern Prime as it equals 7 + 2 * 1 ^ 2".




    • Edited by Blackwood Tuesday, January 29, 2013 4:21 PM corrected the MessageBox text
    Tuesday, January 29, 2013 4:06 PM
  • No ...
    If I input 9 then the answer/output should be 7 + 2 x 1 ^ 2. If 15 then 7 + 2 x 2 ^ 2

    21 then 3 + 2 x 3 ^ 2

    25  then  7 + 2 x 3 ^ 2

    27  then  19 + 2 x 2 ^ 2

    Tuesday, January 29, 2013 4:10 PM
  • No ...
    If I input 9 then the answer/output should be 7 + 2 x 1 ^ 2. If 15 then 7 + 2 x 2 ^ 2

    21 then 3 + 2 x 3 ^ 2

    25  then  7 + 2 x 3 ^ 2

    27  then  19 + 2 x 2 ^ 2

    I see that I left out the "2 *" in the text I displayed in the MessageBox. With that corrected, I think my code displays what you want. Does it not?
    Tuesday, January 29, 2013 4:22 PM
  • Every odd integer inputted, it says "If (odd)  is prime, it is a Stern Prime". And all accepted inputs are odd integers only no even nos.

    I'll just rearrange the codes you gave and try to get the output I need. I really appreciate your help! It's great, Thanks a lot! :)) 

    Tuesday, January 29, 2013 4:28 PM
  • Every odd integer inputted, it says "If (odd)  is prime, it is a Stern Prime". And all accepted inputs are odd integers only no even nos.

    I'll just rearrange the codes you gave and try to get the output I need. I really appreciate your help! It's great, Thanks a lot! :)) 

    I'm sorry, I made a mistake in the test after calling NotSternParts. Here is the corrected code. I also changed it to allow the number to be entered in a TextBox.

    Sub Button2_Click(sender As Object, e As EventArgs)
        Dim checkNumber As Long 
        If Not Long.TryParse(TextBox1.Text, checkNumber) Then
            MessageBox.Show("Enter a valid integer")
            Exit Sub 
        End If
        Dim parts() As Long = NotSternParts(checkNumber)
        If parts(0) = 0 Then
            MessageBox.Show("If " & checkNumber.ToString & " is prime, it is a Stern Prime")
        Else
            MessageBox.Show(checkNumber.ToString & " cannot be a Stern Prime as it equals " _
              & parts(0).ToString & " + " & parts(1).ToString & " ^ 2")
        End If
    End Sub
    

    • Marked as answer by codecodex Tuesday, January 29, 2013 4:50 PM
    Tuesday, January 29, 2013 4:34 PM
  • This is it! :)

    Thanks -- again ..

    Tuesday, January 29, 2013 4:51 PM
  • Hi Blackwood,

    Which bits of your code are needed here?

    Your last code for Button2 and the code from your 2nd post?


    Regards,

    profile for John Anthony Oliver at Stack Overflow, Q&A for professional and enthusiast programmers

    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    App Hub for Windows Phone & XBOX 360 developers.


    Tuesday, January 29, 2013 5:16 PM
  • Sorry, it did get a little confusing with different versions and a couple of mistakes on my part.

    To do what codecodex was asking, you need

    • Button2_Click from my last post (the one codecodex marked as the answer)
    • NotSternParts from my third post (the only one containing that function)
    • NextPrime from my second post
    • IsPrime from my second post

    If you want to calculate all the Stern Primes below a given number, you would just use all the code in my second post. Be aware that, as Devon points out, there are no Stern Primes between 1,493 and 2*10^13, so it isn't really practical to try and find a new one with today's personal computer technology.

    Tuesday, January 29, 2013 5:36 PM