Prime Number Finder in VB.net

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
Welsh Mullet
Posts: 54
Joined: Wed Jan 27, 2010 9:58 pm UTC
Location: South Wales

Prime Number Finder in VB.net

Postby Welsh Mullet » Tue Feb 02, 2010 11:43 pm UTC

I had the wierdest idea that i could write a program to find prime numbers in vb.net.
However, the code i have so far come up with is not working and i cannot understand why, as i am not very experianced.
I suspect it has something to do with the int() part, as this is where the code hangs, but only on the second run through the loop.

Code: Select all

Module Module1

    Sub Main()
        Dim currentValue As Integer = 2
        Dim DivideBy As Integer = 1
        Dim prime As Boolean = True


        Do Until currentValue > 9999999

            DivideBy = currentValue - 1
            prime = True
            Do Until DivideBy = 1 Or prime = False


                If currentValue / DivideBy = Int(currentValue / DivideBy) Then
                    prime = False
                End If


            Loop
            If prime = True Then
                Console.Write(currentValue)
                Console.WriteLine()
            End If

            currentValue = currentValue + 1

        Loop


    End Sub

End Module
Last edited by Welsh Mullet on Wed Feb 03, 2010 11:52 pm UTC, edited 1 time in total.

User avatar
Sizik
Posts: 1255
Joined: Wed Aug 27, 2008 3:48 am UTC

Re: Check for whole number in VB.net

Postby Sizik » Wed Feb 03, 2010 12:03 am UTC

You're not decrementing DivideBy in the inner loop, so it's just looping infinitely.
she/they
gmalivuk wrote:
King Author wrote:If space (rather, distance) is an illusion, it'd be possible for one meta-me to experience both body's sensory inputs.
Yes. And if wishes were horses, wishing wells would fill up very quickly with drowned horses.

User avatar
Welsh Mullet
Posts: 54
Joined: Wed Jan 27, 2010 9:58 pm UTC
Location: South Wales

Re: Check for whole number in VB.net

Postby Welsh Mullet » Wed Feb 03, 2010 10:51 pm UTC

it works now, and finds primes...not sure what to do with it though...
Leave it running I suppose. What's the highest prime found? What do I do if I find a higher one?

fazzone
Posts: 186
Joined: Wed Dec 10, 2008 9:38 pm UTC
Location: A boat

Re: Check for whole number in VB.net

Postby fazzone » Wed Feb 03, 2010 11:40 pm UTC

As it happens, finding really large primes is interesting in cryptography, so there are people who stand to make a substantial amount of money by finding really large primes. I think you can sell primes that are more than 10000 digits to the NSA or something. Anyway, as I recall, the largest known prime is about 13 million digits long in base-10.
*/

User avatar
Xanthir
My HERO!!!
Posts: 5413
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Check for whole number in VB.net

Postby Xanthir » Wed Feb 03, 2010 11:45 pm UTC

Welsh Mullet wrote:it works now, and finds primes...not sure what to do with it though...

1) Use your new knowledge of primes to solve Project Euler problems.
2) Develop more efficient prime-finders, thus increasing your skill and knowledge of algorithms and data structures. The algorithm you're using right now, frex, is very nearly the worst not-stupid-on-purpose algorithm you can create. There are *vastly* faster methods out there, and even sticking with roughly your current algorithm, you can massively increase its speed with some pretty simple math tweaks. Learning the how and why of those is pretty fun.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
Welsh Mullet
Posts: 54
Joined: Wed Jan 27, 2010 9:58 pm UTC
Location: South Wales

Re: Check for whole number in VB.net

Postby Welsh Mullet » Wed Feb 03, 2010 11:49 pm UTC

Ah... that plan kinda goes down the drain then, unless i set up a pc with a huge amount of storage and run the program starting at 9 + 12 million 9's


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 5 guests