Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
need to create a loop to calculate the number of prime factors of any given number.
basically i have tried to make a loop using modulus to calculate whether any number is a prime number
for(i=1;i<=input_number; i++)
{
if (input_number%i == 0)
number_of_divisors++;
}
if (number_of_divisors == 2)//primenumber.
then if it is a prime number find if it is a prime factor of the input number.
a%primenumber==0 //a=input number
if this is true then update a counter to keep track of prime factors.
if this sin't the best approach since i can't get an output is their a better way to go around itadriantheo

I hope I'm not doing your homework for you. But here goes.
First of all you do not need your loop to check 1 and the number itself - you know that it will be divisible by them. Instead start at 2. And when a factor is found, divide the input number by the factor and start the counter over again at 2. That will cut down on the processing time as you wont need to go through the loop as many times - especially for big numbers. For example, for the number 100 the first time through it would find that 2 is a PF. It would then start over again with 2 using 50. Again, it would find 2 to be a PF of 50. The loop would start over again with 25. The loop would go through 8 more times to find that 25 has two PFs of 5 and 5 - for a total of 10 loops instead of 100.
Here is an example of how the code would work. I have added an array to hold the prime factors. When the loop completes, you can test if the number is prime if the number_of_divisors is 0, otherwise the array contains all of the prime factors.
Code:
********************************
tempNumber=input_number //To refer back to orig.
pfArray = new Array; //Will hold the prime factors
number_of_divisors = 0for(i=2;i<tempNumber; i++) {
if (tempNumber%i == 0) {
pfArray[number_of_divisors] = i;
number_of_divisors++;
tempNumber = tempNumber / i;
i = 1;
}
}if (number_of_divisors!=0) {
pfArray[number_of_divisors] = i; //add the last prime factor
msg = "The prime factors for this number are: \n"
for (i=0; i<pfArray.length; i++) {
msg = msg + pfArray[i] + ', ';
}
alert(msg);
} else {
alert('The number is prime');
}
********************************Michael J

Seriously, I just did this as a part of my homework. I finally figured it out and while I am still going to post this, I hope you do not use this to do your homework. Maybe it can be legitimately used as a guide though. I created the program in Java:
This may not be the most efficient way but it is the easiest way I have seen from searches on the subject. I figured this out from a hint my professor gave us in class. That and some good old thinking finally lead me to an easy way to do it. I threw up a simple (Very simple!) site, go to it and look at Prime.java down at the bottom. It explains a bit about it too. The site is here:
home.tampabay.rr.com/johnsiteI hope this helps. -Johnathan
PS: It would be funny if we were in the same class!

![]() |
check date before removin...
|
Indirect pointers in VB.N...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |