Answered by:
Stern Prime
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
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
All replies



[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 nonzero integer ' In other words q <> p = 2 * b ^ 2 (where p and q are prime and b is a nonzero 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


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 nonzero integer ' In other words q <> p = 2 * b ^ 2 (where p and q are prime and b is a nonzero 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


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


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

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


No ...
If I input 9 then the answer/output should be 7 + 2 x 1 ^ 2. If 15 then 7 + 2 x 2 ^ 221 then 3 + 2 x 3 ^ 2
25 then 7 + 2 x 3 ^ 2
27 then 19 + 2 x 2 ^ 2

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! :))

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


Hi Blackwood,
Which bits of your code are needed here?
Your last code for Button2 and the code from your 2nd post?
Regards,
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.
 Edited by John Anthony Oliver Tuesday, January 29, 2013 5:17 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.