# Prime numbers • ### Question

• Hello

I need to write a program, that takes a number (entered into a textbox, by the user) and determines if the number is prime or not. If the number is a prime number, then the program is just supposed to display, something like "This is a prime number" in a label. If the number entered is not a prime number, then the program is supposed to display the prime factors of that number. Must use the following Function and Procedure in program:

• A function that takes a single Integer parameter and returns a boolean value. True if the parameter is prime, False otherwise.
• A procedure that takes an Integer parameter and computes and writes out the prime factors of that parameter.

If you are not familiar with prime numbers and/or prime factors, there is a explanation of them below.

Prime numbers are numbers that can only be divided evenly by 1 and the number itself. For example, 7 is a prime number since there is no number less than 7 (except for 1) that divides evenly into it.

Numbers that are not prime numbers can be reduced to a product of prime factors. For example 15 is not a prime number but it can be expressed as (3 * 5) i.e. as set of prime numbers multiplied together. Note that a prime factor may occur more than once. For example 16 is 2 * 2 * 2 * 2.

Ok, I understand the algorithm or pseudo code of this problem. But the part of this problem that I do not understand right now, is how do I determine or test whether a number is evenly divisble or not. (9/3=3) (4/2=2) (10/5=2)

Thanks.

Thursday, December 1, 2005 12:06 AM

• Asking for help with homework is a bit off-topic.  I'd prefer you ask how to do something specific with the langage, rather than asking for algorithms.

With that said, I'll be a little helpful.  Here are the pieces of code you need for each aspect of working with arraylists in VB.  You can use these to solve your problem.

'define arraylist

Dim primefactors As New System.Collections.Generic.List(Of Integer)

'example of an integer

Dim i As Integer = 5

'add a value to the arraylist

'loop through arraylist values

For Each factor As Integer In primefactors

'display factor

Debug.print(factor)

Next

Enjoy.

Paul

Friday, December 16, 2005 10:57 PM

### All replies

• Hello.

There is an opertor "Mod" that let's you check for a rest after a division. Also, prime-factors can be no larger than the square root of the number you are checking, so

http://en.wikipedia.org/wiki/Trial_division

Thursday, December 1, 2005 10:06 AM
• Ok, I figured out the first part, where it just has to be determined, whether the user entered number is prime or not. I already handed in
the assignment, with the first part done, because it was due today
(Wed, Dec 7, 2005).

This is what I did for the first part:
Do while i < inputnum
if inputnum mod i = 0
'then its not prime
else
i = i + 1
End If
If inputnum mod i = 0 Then
Exit For
End if
' Number is prime
Loop

But I could not figure out how to do the second part. All I know about
the second part, is that I should use arrays (there probably are many
ways to do it, but I have to use arrays, because that is what I have
learned about). The second part is where, if a non prime number is
entered, and the program has to display all prime factors of the
number. Ex: 3 * 5 = 15.
But I feel that, I should still figure out the second part, for future
reference. It might also help me out on the exam that is coming up for
this class.

So can somebody help me with the second part?

Thursday, December 8, 2005 1:20 AM
• Asking for help with homework is a bit off-topic.  I'd prefer you ask how to do something specific with the langage, rather than asking for algorithms.

With that said, I'll be a little helpful.  Here are the pieces of code you need for each aspect of working with arraylists in VB.  You can use these to solve your problem.

'define arraylist

Dim primefactors As New System.Collections.Generic.List(Of Integer)

'example of an integer

Dim i As Integer = 5

'add a value to the arraylist

'loop through arraylist values

For Each factor As Integer In primefactors

'display factor

Debug.print(factor)

Next

Enjoy.

Paul

Friday, December 16, 2005 10:57 PM