Prime Number 'Producer'- Can't get it to work

Page 1 of 1

11 Replies - 3304 Views - Last Post: 02 November 2012 - 09:12 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=298059&amp;s=0c5d4b8a4464e4608c576c96320a2f83&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 Shadow_Phoenix

• New D.I.C Head

Reputation: 0
• Posts: 6
• Joined: 31-October 12

Prime Number 'Producer'- Can't get it to work

Posted 31 October 2012 - 08:07 AM

Hello guys!

As the title implies, I need help on a "Prime Number Producer". By that I mean the following: If someobody enters a number, they are supposed to get shown all the prime numbers up to the entered number.

This is what it's supposed to look like.

Here's my code:
```        Dim Maximum As Double
Dim i As Integer
Dim y As Integer
Dim Prime As Boolean

For Maximum = 3 To y - 1 Step 2
If y Mod Maximum = 0 Then
Prime = False
Exit For
For i = 3 To Math.Sqrt(y) Step 2
Prime = True
lst_Prime.Items.Add(i)
Next
End If
Next Maximum
End Sub

```

The problem is that it isn't giving me out any numbers. I have tried checking it but I just can't seem to find the error ;. I tried checking out books, browsing forums but nothing has helped.

Do not worry, this is not supposed to be homework. It is merely a practice for me, programming-wise, and for somebody who wants to help teaching their children the prime numbers (so that they can double-check when they practise).

I thank you for your help in advance! :-)

Is This A Good Question/Topic? 0

Replies To: Prime Number 'Producer'- Can't get it to work

#2 modi123_1

• Suitor #2

Reputation: 13962
• Posts: 55,730
• Joined: 12-June 08

Re: Prime Number 'Producer'- Can't get it to work

Posted 31 October 2012 - 08:20 AM

Do you have any code that connects a button click to that code above?
Was This Post Helpful? 0

#3 lucky3

• Friend lucky3 As IHelpable

Reputation: 233
• Posts: 770
• Joined: 19-October 11

Re: Prime Number 'Producer'- Can't get it to work

Posted 31 October 2012 - 08:21 AM

Is this in your button.click handler method, or is this another sub, or perhaps function without return? You can't have result, if you don't use what your routine does. Do something with this boolean result (TRUE/FALSE).
Was This Post Helpful? 0

#4 CharlieMay

• This space intentionally left blank

Reputation: 1728
• Posts: 5,707
• Joined: 25-September 09

Re: Prime Number 'Producer'- Can't get it to work

Posted 31 October 2012 - 09:47 AM

What's the Exit For for? You can't get to the add method of the listbox because you are exiting the for loop.
Was This Post Helpful? 0

#5 Shadow_Phoenix

• New D.I.C Head

Reputation: 0
• Posts: 6
• Joined: 31-October 12

Re: Prime Number 'Producer'- Can't get it to work

Posted 31 October 2012 - 11:57 AM

@modi123_1
Yes, I do have the connections to the buttons. However, I did decide not to post them so that it would be a little more time-saving to look over it.

@lucky3
This is only the calculation itself. Thanks for the hint! I will be sure to look into that again.

@CharlieMay
I thought adding the "Exit for" was necessary to end the If.

Just to make sure, here's the full code

```Public Class Form1

'Exit the Programme
Private Sub cmd_Beenden_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_Beenden.Click
Me.Close()
End Sub

'Procedure to  calculate prime numbers up to the entered number
Private Sub cmd_Berechnen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_Berechnen.Click
'Declaration of the  Variables
Dim Maximum As Double
Dim i As Integer
Dim y As Integer
Dim Prime As Boolean

'Clean  Listbox
lst_Prime.Refresh()

'Initialising the entered Variables
Maximum = txt_Maximum.Text

For Maximum = 3 To y - 1 Step 2
If y Mod Maximum = 0 Then
Prime = False
Exit For
For i = 3 To Math.Sqrt(y) Step 2
Prime = True
lst_Prime.Items.Add(i)
Next
End If
Next Maximum
End Sub
'Procedure, to let the Calculate-Button react to Enterkey
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.AcceptButton = cmd_Berechnen
End Sub

Private Function Program() As Object
Throw New NotImplementedException
End Function

Private Function Code() As Object
Throw New NotImplementedException
End Function

End Class

```

I'm sorry if posting nothing but the calculation was troublesome ;. Thank you all for your fast replies!
Was This Post Helpful? 0

#6 modi123_1

• Suitor #2

Reputation: 13962
• Posts: 55,730
• Joined: 12-June 08

Re: Prime Number 'Producer'- Can't get it to work

Posted 31 October 2012 - 12:04 PM

Just for kicks - what happens when you put a breakpoint on line 23 and walk through your for-loop?
Was This Post Helpful? 1

#7 lucky3

• Friend lucky3 As IHelpable

Reputation: 233
• Posts: 770
• Joined: 19-October 11

Re: Prime Number 'Producer'- Can't get it to work

Posted 31 October 2012 - 12:11 PM

As CharlieMay pointed to, let me walk through the code for you (and before I forget, there's a great tutorial about debugging here):

```23	        For Maximum = 3 To y - 1 Step 2
24	            If y Mod Maximum = 0 Then 'if this is TRUE, you want to do all the lines in if, and if it's FALSE, you don't do any of them, because there's no Else or Else If in this If statement
25	                Prime = False 'OK, you got True in the line above, and now you're setting Prime to False
26	                Exit For 'and here you exit the For loop each time, and none of the code lines below this line, in this If statement, never executes!

```

Edit: sorry modi123_1, I unintentionally spoiled your hint. I was searching for this tutorial, to point him to, while you were writing.

This post has been edited by lucky3: 31 October 2012 - 12:16 PM

Was This Post Helpful? 1

#8 LilGhost

• D.I.C Head

Reputation: 8
• Posts: 98
• Joined: 12-October 12

Re: Prime Number 'Producer'- Can't get it to work

Posted 31 October 2012 - 01:48 PM

```    Private Sub cmd_Berechnen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_Berechnen.Click
'Declaration of the  Variables
Dim Maximum As Double
Dim i As Integer
Dim y As Integer
Dim Prime As Boolean

'Clean  Listbox
'lst_Prime.Refresh()
lst_Prime.items.clear()

'Initialising the entered Variables
Maximum = txt_Maximum.Text

For Maximum = 3 To y - 1 Step 2
If y Mod Maximum = 0 Then
Prime = False
Exit For
else
For i = 3 To Math.Sqrt(y) Step 2
Prime = True
lst_Prime.Items.Add(i)
Next
End If
Next Maximum
End Sub

```

Okay, so this looks a lot like your code. The reason for this is that I really only noticed one section that could cause an error of no output. In your:
```If y mod maximum = 0 then
```

command you had the following:
```            If y Mod Maximum = 0 Then
Prime = False
Exit For
For i = 3 To Math.Sqrt(y) Step 2
Prime = True
lst_Prime.Items.Add(i)
Next
End If

```

the reason this doesn't work is that it needs an else statement so it know "if y mod..." isn't true and thus it is prime, I need to add this to my list box. Otherwise it's only checking the if statement, if the if statement is false, it moves on to an else/elseif. Since you had no else/elseif it assumed it was done and moved onto "End if" which then told it to end the if (obviously). Thus, it never knew it as supposed to execute the for loop.

Also, you had
```lst_Prime.refresh()

```

I haven't ever used that function so i swapped it with:
```lst_Prime.items.clear()

```

which i know works.
Was This Post Helpful? 1

#9 Shadow_Phoenix

• New D.I.C Head

Reputation: 0
• Posts: 6
• Joined: 31-October 12

Re: Prime Number 'Producer'- Can't get it to work

Posted 01 November 2012 - 06:49 AM

@modi123_1
That sure was a helpful hint, thanks! I can't believe I forgot about the debugging function.

@lucky3
Thanks to you as well for the helpful link about debugging.
As for the code, I know where you're coming from but whenever I change it (and use the Code
LilGhost made, I still don't get any output x.x)

@LilGhost
Thanks, it's good to know that I only messed up the else. However, I tried running it and I still
didn't get an output on the list. Again thank you for your great explanations.
It could be that your lst_Prime.items.clear() works better than what I had before.

Again, I thank you all very much for your patience with me :-)
Was This Post Helpful? 0

#10 lucky3

• Friend lucky3 As IHelpable

Reputation: 233
• Posts: 770
• Joined: 19-October 11

Re: Prime Number 'Producer'- Can't get it to work

Posted 01 November 2012 - 07:09 AM

Well, you need to actually display lst_Prime.Items somewhere, after you finish with For loop. And BTW, the logic of finding primes is wrong, but that you will discover and adjust, when you see primes form lst_Prime displayed.
Was This Post Helpful? 0

#11 Shadow_Phoenix

• New D.I.C Head

Reputation: 0
• Posts: 6
• Joined: 31-October 12

Re: Prime Number 'Producer'- Can't get it to work

Posted 01 November 2012 - 11:30 AM

Ok, I put the lst-Prime.Items in the right place (that problem's solved now) but I still can't seem to find the error in the calculation itself. I'm sorry, the flu seem to have gotten the best of me! ;
Was This Post Helpful? 0

#12 Shadow_Phoenix

• New D.I.C Head

Reputation: 0
• Posts: 6
• Joined: 31-October 12

Re: Prime Number 'Producer'- Can't get it to work

Posted 02 November 2012 - 09:12 AM

I just wanted to let you guys know that I solved it!
Thank you ever so much! :-)
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }