Jump to content

Featured Replies

Posted

Hey people, I was working on a program in Visual Basic that 'supposingly' generates a list of Prime Numbers from 0 to a number you put in.

I've encountered some problems though.

At the moment when you execute the program, type in a number and press the button to start the calculation nothing happens.

I'm sure it's probably something very stupid... but if you know the awnser please tell me.

 

Private Sub cmdSearch_Click()    'the button you pressed to start
'txtText1 is the input textfield in which you type in the max. number to look for primenumbers for
'txtText2 is the output textfield that displays all the found numbers

Dim Teller As Long
Dim Half As Long
Dim Root As Long
Dim Counter1 As Long
Dim Counter2 As Long
Dim Number1 As Long
Dim Number2 As Long

If IsNumeric(txtText1.Text) Then
   Teller = CLng(txtText1.Text)
   If Teller > 0 And Teller < 1000000000 Then
       txtText2.Text = "1, 2"
       For Counter1 = 3 To Teller
           If Counter1 / 2 = Round(Counter1 / 2) Then
           Else
               Root = Round(Sqr(Counter)) + 1
               For Counter2 = 3 To Root
               Number1 = Counter1 / Counter2
               Number2 = Round(Counter1 / Counter2)
               If Number1 = Number2 Then
                   Counter2 = Root - 1
               Else
               If Counter2 = Root - 1 And Not (Round(Counter1 / Counter2) = Counter1 / Counter2) = True Then
                   txtText2.Text = txtText2.Text + ", " + Counter
               End If
               End If
               Next Counter2
           End If
       Next Counter1
   Else
       txtText2.Text = "You have to type a POSITIVE NUMBER between ZERO and ONE BILION in the field above!!"
   End If
Else
   txtText2.Text = "You have to type a POSITIVE NUMBER between ZERO and ONE BILION in the field above!!"
End If

End Sub

Hey people, I was working on a program in Visual Basic that 'supposingly' generates a list of Prime Numbers from 0 to a number you put in.

I've encountered some problems though.

At the moment when you execute the program, type in a number and press the button to start the calculation nothing happens.

I'm sure it's probably something very stupid... but if you know the awnser please tell me.

 

Private Sub cmdSearch_Click()    'the button you pressed to start
'txtText1 is the input textfield in which you type in the max. number to look for primenumbers for
'txtText2 is the output textfield that displays all the found numbers

Dim Teller As Long
Dim Half As Long
Dim Root As Long
Dim Counter1 As Long
Dim Counter2 As Long
Dim Number1 As Long
Dim Number2 As Long

If IsNumeric(txtText1.Text) Then
   Teller = CLng(txtText1.Text)
   If Teller > 0 And Teller < 1000000000 Then
       txtText2.Text = "1, 2"
       For Counter1 = 3 To Teller
           If Counter1 / 2 = Round(Counter1 / 2) Then
           Else
               Root = Round(Sqr(Counter)) + 1
               For Counter2 = 3 To Root
               Number1 = Counter1 / Counter2
               Number2 = Round(Counter1 / Counter2)
               If Number1 = Number2 Then
                   Counter2 = Root - 1
               Else
               If Counter2 = Root - 1 And Not (Round(Counter1 / Counter2) = Counter1 / Counter2) = True Then
                   txtText2.Text = txtText2.Text + ", " + Counter
               End If
               End If
               Next Counter2
           End If
       Next Counter1
   Else
       txtText2.Text = "You have to type a POSITIVE NUMBER between ZERO and ONE BILION in the field above!!"
   End If
Else
   txtText2.Text = "You have to type a POSITIVE NUMBER between ZERO and ONE BILION in the field above!!"
End If

End Sub

 

First thing I found wrong,

 

Root = Round(Sqr(Counter)) + 1

 

Im thinking you meant to put Counter1

 

but the whole thing is just horrbly wrong, nothing works, I dont see how anything would.

 

try googling for it.

it seems like the basic logic would be something like this.

for(i=3;i<input_number;i++)
{ 
  for(j=(input_number/2);j > 1; j++)
   {
       divide input by j
       if (answer is whole number)
            set flag                             ;This means it's not prime
            if (flag)
               jump next
   }
  Print out input_number
next:                              ;puts you at the end of the loop, so i increases.
}

 

Go from there. you figure the rest out.

  • Author

Ok ppl, thnx for the input. I've found that I used some wrong variables yes, but fixing that doesn't completely solve the problem.

I've run some tests and found that if you take i.e. 'number/2' it automatically rounds it off to a complete number. And since the code is meant to ignore all 'even' numbers (which have a round number if you divide it by 2) it ignores all numbers since all numbers end up to have a round number divided by 2.

I'm not completely sure on how to make it so it doesn't round off anything, and/or what variable type(s) to use that allow decimals.

 

My current code:

Private Sub cmdSearch_Click()

Dim Teller, Half, Root, Counter1, Counter2, Number1, Number2 As Long

If IsNumeric(txtText1.Text) Then
   Teller = CLng(txtText1.Text)
   If Teller > 0 And Teller < 1000000000 Then
       txtText2.Text = "1, 2"
       For Counter1 = 3 To Teller
           If (Counter1 / 2) = Round(Counter1 / 2) Then
           Else
               Root = Round(Sqr(Counter1)) + 1
               For Counter2 = 3 To Root
               Number1 = (Counter1 / Counter2)
               Number2 = Round(Counter1 / Counter2)
               If Number1 = Number2 Then
                   Counter2 = Root - 1
               Else
               If Counter2 = (Root - 1) And Not (Round(Counter1 / Counter2) = (Counter1 / Counter2)) Then
                   txtText2.Text = txtText2.Text + ", " + CStr(Counter1)
               End If
               End If
               Next Counter2
           End If
       Next Counter1
   Else
       txtText2.Text = "You have to type a POSITIVE NUMBER between ZERO and ONE BILION in the field above!!"
   End If
Else
   txtText2.Text = "You have to type a POSITIVE NUMBER between ZERO and ONE BILION in the field above!!"
End If

End Sub

  • Author

just tried, didn't work.

 

Anyway when I run the program it does use up 100% CPU so it is doing something.. it eventually gets stuck though and I have to force end it.

Perhaps there is a bug in one (or both) of the loops. Anyway I've just finished downloading MSDN Library so will install that asap and see if I can find any info on there.

 

EDIT: Yet annother code 'update'.. dunno if it is in the right direction, cuz I get no change (btw I changed first For loop to '10 to teller' to perhaps fish out any problems the '3 to root' 2nd For loop could have if root was lesser than 3):

Private Sub cmdSearch_Click()

Dim Teller, Half, Root, Counter1, Counter2, Number1, Number2 As Long

If IsNumeric(txtText1.Text) Then
   Teller = CLng(txtText1.Text)
   If Teller > 0 And Teller < 1000000000 Then
       txtText2.Text = "1, 2"
       For Counter1 = 10 To Teller
           Number1 = Round((Counter1 / 2), 10)
           Number2 = Round((Counter1 / 2), 0)
           If Number1 = Number2 Then
           Else
               Root = (Round((Sqr(Counter1)), 0) + 1)
               For Counter2 = 3 To Root
                   Number1 = Round((Counter1 / Counter2), 10)
                   Number2 = Round((Counter1 / Counter2), 0)
                   If Number1 = Number2 Then
                       Counter2 = (Root - 1)
                   Else
                   If Counter2 = (Root - 1) And Not Number1 = Number2 Then
                       txtText2.Text = txtText2.Text + ", " + CStr(Counter1)
                   End If
                   End If
               Next Counter2
           End If
       Next Counter1
   Else
       txtText2.Text = "You have to type a POSITIVE NUMBER between ZERO and ONE BILION in the field above!!"
   End If
Else
   txtText2.Text = "You have to type a POSITIVE NUMBER between ZERO and ONE BILION in the field above!!"
End If

End Sub

an idea for the prime numbers...

 

 

take a number and divide it as an integer. I.E num = 5, current division =2

 

5/2 = 2.5

 

Since it's an integer, it's going to chop off whatever is the last bit of the decimal.

 

2.5=2.

 

Then multiply the resulting number by what you origianlly divided by.

 

2*2 = 4

 

compare, and if not equal, it's not prime

 

4!=5, so we're good.

 

and you're getting stuck in an infinite loop.. step though your loops to see where you went wrong.

  • Author

ah! that's a very good idea megafighter, I never thought of that.

That fixes the rounding off problem alltogether.:boink:

 

Now for the infinite loop... hmmm:ugh:

 

 

EDIT: OMFG WOOOT IT WORKS!!!!!!!!!!!!!!!!!!!! *does a happy dance*

 

Now it'll probably need some tweaking and optimising, but atleast it is displaying prime numbers and not looping forever:

 

Private Sub cmdSearch_Click()

Dim Teller, Half, Root, Counter1, Counter2, Number1, Number2 As Long

If IsNumeric(txtText1.Text) Then
   Teller = CLng(txtText1.Text)
   If Teller > 0 And Teller < 1000000000 Then
       For Counter1 = 3 To Teller
           Number1 = Round(Counter1 / 2)
           Number2 = Number1 * 2
           If Number2 = Counter1 Then
           Else
               Root = Round(Sqr(Counter1)) + 1
               For Counter2 = 3 To Root
                   Number1 = Round(Counter1 / Counter2)
                   Number2 = Round(Number1 * Counter2)
                   If Number2 = Counter1 Then
               Exit For
                   Else
                   If Counter2 = Root And Not Number2 = Counter1 Then
                       txtText2.Text = txtText2.Text + ", " + CStr(Counter1)
                   End If
                   End If
               Next Counter2
           End If
       Next Counter1
   Else
       txtText2.Text = "You have to type a POSITIVE NUMBER between ZERO and ONE BILION in the field above!!"
   End If
Else
   txtText2.Text = "You have to type a POSITIVE NUMBER between ZERO and ONE BILION in the field above!!"
End If

End Sub

  • Author

Ok, I did a bit of tweaking nd stuff, but it's not completely done yet.

If you wanna have a look here's a link to the compiled executable:

http://members.home.nl/frankvandermast/Project1.exe

 

Also I've found the maximum number you'll get displayed in the text2.text is 99709 even if you declare the max. value to search for as a milion or bigger. It will calculate up to a milion but just display untill 99709. Perhaps a limitation in the textfield? Also it doesn't display the number untill it's completely finished calculating all the primes.. it should be updating the list every time a new prime is found. Perhaps this is the problem, my memory can only store the text up to 99709?

 

EDITED @ 28/01/2006, 17:22GMT : Ok guys I did some upgrades to the program again, made it a bit more complex. You can now insert a starting value for the list generator and I also added in a function to insert a number and let it check if inserted number is prime. Still haven't fixed the problem stated above though, but the 'checker' appears to accept larger numbers than 100000. Still limited by the 'Long' variable type though. I searched a bit about defining your own variable types with the 'Type' command or sumthing, but all I could find was rubbish o.O

Ok, I did a bit of tweaking nd stuff, but it's not completely done yet.

If you wanna have a look here's a link to the compiled executable:

http://members.home.nl/frankvandermast/Project1.exe

 

Also I've found the maximum number you'll get displayed in the text2.text is 99709 even if you declare the max. value to search for as a milion or bigger. It will calculate up to a milion but just display untill 99709. Perhaps a limitation in the textfield? Also it doesn't display the number untill it's completely finished calculating all the primes.. it should be updating the list every time a new prime is found. Perhaps this is the problem, my memory can only store the text up to 99709?

 

EDITED @ 28/01/2006, 17:22GMT : Ok guys I did some upgrades to the program again, made it a bit more complex. You can now insert a starting value for the list generator and I also added in a function to insert a number and let it check if inserted number is prime. Still haven't fixed the problem stated above though, but the 'checker' appears to accept larger numbers than 100000. Still limited by the 'Long' variable type though. I searched a bit about defining your own variable types with the 'Type' command or sumthing, but all I could find was rubbish o.O

 

how are you "limited by the 'Long' variable type" ? it can hold pretty big numbers ( ±2^63 )

  • Author

Well, my end goal is that the program will be able to just automatically assign the needed memoryspace, i.e. if it needs 8bytes it assigns 8 bytes and if it needs 16 it assigns 16 etc. I don't know if this is possible but that's what I want.

 

Also I need a way to fix the problem with the text field because it will only display prime numbers from start value to ~start value + 100.000. I believe this is due to the nature of the textfield and it's way of storing data.. don't know how to edit/update/change/check that though. Btw I've also uploaded the compiled program of a prime numbers generator from codeguru. I found it is a bit sloppy though cuz the 'save list as .txt' doesn't work (or atleast not for me) and it displays a list of 'prime number counter', 'actual prime number'. I just want it to display 'prime number', 'next prime number' as in my program. ALSO I tried letting that program calculate the prime numbers from 0 to 10.000 and with my program. I found that with my program it takes about 2 seconds, and with his it takes about 250 seconds. wtf is up with that?!

 

Link to my program: http://members.home.nl/frankvandermast/Project1.exe

Link to his program: http://members.home.nl/frankvandermast/Primes.exe

Link to my current source code: http://members.home.nl/frankvandermast/code.txt

Guest
This topic is now closed to further replies.