Using A Loop/Array To Find Prime Numbers

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 12572 Views - Last Post: 10 October 2012 - 02:21 PM Rate Topic: -----

#16 CodyMcCoderson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 08-October 12

Re: Using A Loop/Array To Find Prime Numbers

Posted 09 October 2012 - 04:27 PM

Ok, So I made a couple changes, I dont know if they're correct, but thats why im trying.
But why does inputNum under Private Function not work, even though its dim under the module and not the sub?

Maybe I dont have this correct, but i've also been steadily working on 4 other questions for programming at the same time and this is the only one giving me a world of trouble and its getting to me. haha

You're help is greatly appreciated though and hopefully i'll get this soon. haha

Module Module1
    Dim inputNum As String = 0
    Sub Main()

        Dim primeNumbers As String
        Do While inputNum <> "end"
            Console.WriteLine(vbNewLine & "Please enter a number: ( 'end' to stop)")
            primeNumbers = ""
            inputNum = Console.ReadLine()
            If inputNum = "end" Then End
            Dim i As Integer = 1
            If isPrime(i) Then primeNumbers = i
            i += 1
            If isPrime(i) Then primeNumbers = i
            'For i = 3 To inputNum 'not inputNum-1, because we want to test inputNum as well
            '    If isPrime(i) Then
            '        primeNumbers &= "," & i
            '    End If
            'Next
            For i = 3 To inputNum Step 2
                If isPrime(i) Then
                    primeNumbers &= "," & i
                End If
            Next


            Console.WriteLine(vbNewLine & "Prime numbers up to " & inputNum)
            Console.WriteLine(primeNumbers)
        Loop
    End Sub

    Private Function isPrime(ByVal testnum As Integer) As Boolean
        Dim ctr As Integer
        If testnum = 1 Then Return False
        If testnum = 2 Then Return True
        If testnum Mod 2 = 0 Then Return False
        isPrime = True

        For inputNum = 3 To testnum
            If testnum Mod ctr = 0 Then
                isPrime = False
            End If
        Next
    End Function


End Module


Was This Post Helpful? 0
  • +
  • -

#17 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Using A Loop/Array To Find Prime Numbers

Posted 09 October 2012 - 05:45 PM

View PostCodyMcCoderson, on 09 October 2012 - 04:27 PM, said:

Ok, So I made a couple changes, I dont know if they're correct, but thats why im trying.
But why does inputNum under Private Function not work, even though its dim under the module and not the sub?


We cannot use inputNum as a counting variable in a For loop because it is a String. See further down for comments about the For loop in isPrime().

Fortunately, we don't require inputNum within the isPrime function. Why? Because in this function, we don't care about inputNum. The only number we care about is the one we pass to it, which is a test number. What we are doing is passing all the odd numbers from 3 to the value of imputNum, one at a time, and asking if it is prime or not. In the function declaration, after the ')', it says 'As Boolean', which means that the function MUST return a True or a False.

Quote

Maybe I dont have this correct, but i've also been steadily working on 4 other questions for programming at the same time and this is the only one giving me a world of trouble and its getting to me. haha

You're help is greatly appreciated though and hopefully i'll get this soon. haha


We'll be there soon. I promise.

Quote

    Private Function isPrime(ByVal testnum As Integer) As Boolean
        Dim ctr As Integer
        If testnum = 1 Then Return False
        If testnum = 2 Then Return True
        If testnum Mod 2 = 0 Then Return False
        isPrime = True

        For inputNum = 3 To testnum
            If testnum Mod ctr = 0 Then
                isPrime = False
            End If
        Next
    End Function


The inner For loop here is saying:
assign a value of 3 to inputNum
each time through the loop, increment inputNum by +1
leave the loop when we reach the value in testNum.

As you can see, asking a string to be a numeric counter is not going to work.

So, here we have testNum, passed into the isPrime() function. Notice we have set a variable called isPrime to True. There are (at least) two ways to return a value from a function. The first is to explicitly say "Return <value>", where <value must be either a variable of the proper type, or a value of that type. Up further where we say 'Return True', is an example of this method.

The other way is to assign a value to the name of the function, so 'isPrime = True' will retrun True to the caller without having to explicitly telling it what to return. It will return that value by saying 'Return', or by just 'falling out the bottom' of the function. The reason we set isPrime to True is because it is easier to prove a number non-prime than to prove it prime.

Except for the use of inputNum you came pretty close on the inner loop! Good job!

Try this:
        For ctr = 3 To testnum \ 2 Step 2
            If testnum Mod ctr = 0 Then
                isPrime = False
                Exit For
            End If
        Next


There are two things extra in there, but both are merely optimizations for speed and to avoid unnecessary operations.
we count ctr from 3 to 'testnum \ 2' because a number cannot be evenly divisible by a number more than half of it. What thes loop says is:
assign 3 to ctr
each time through, increment by +2 (to divide by only odd numbers)
leave the loop when we reach the integer value of testnum divided by 2

The 'Exit For' is there because once we find a number that divides evenly into testnum, there is no sense trying any further, because the number is already proven to be non-prime. An example. testnum = 99, and the first test is '99 mod 3', which will result in 0, so we only had to do one test on that number, instead of 25 tests or so.

When we 'fall out the bottom' of the function, it will return a True if the number is prime, or a False if the number is non-prime.

And there you go, a working program.
Study it in detail, perhaps using a breakpoint and stepping through one line at a time. If you understand what all is happening in there, it will go a long way toward making your next program easier.

Edited for spelling.

This post has been edited by lar3ry: 09 October 2012 - 07:55 PM

Was This Post Helpful? 2
  • +
  • -

#18 CodyMcCoderson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 08-October 12

Re: Using A Loop/Array To Find Prime Numbers

Posted 10 October 2012 - 10:04 AM

By reading what you just wrote, I understand what the hell is going on now 100% more then when my instructor explained it. He's so vague and simplistic. Now that you gave each word a meaning it really helped.

Thank you very, very much!
Was This Post Helpful? 0
  • +
  • -

#19 PeterH  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 47
  • Joined: 03-September 09

Re: Using A Loop/Array To Find Prime Numbers

Posted 10 October 2012 - 02:06 PM

For what its worth, I thought the manner in which lar3ry provided such excellent assistance during this post is to be highly praised. Patience, great explanations and an understanding of how to teach learners, without some of the patronising, dismissive or plain arrogant replies sometimes forthcoming.

Nice one lar3ry
Was This Post Helpful? 0
  • +
  • -

#20 CodyMcCoderson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 08-October 12

Re: Using A Loop/Array To Find Prime Numbers

Posted 10 October 2012 - 02:21 PM

He did it exactly how people trying to help should.

He explained, rather then just giving me the code.

Now I understand what im doing.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2