Computing.Net > Forums > Programming > loop to calculate # of prime factor

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

loop to calculate # of prime factor

Reply to Message Icon

Name: adriantheo
Date: April 5, 2005 at 23:39:42 Pacific
OS: 002
CPU/Ram: 02
Comment:

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 it

adriantheo




Sponsored Link
Ads by Google

Response Number 1
Name: Michael J (by mjdamato)
Date: April 6, 2005 at 04:54:24 Pacific
Reply:

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 = 0

for(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


0

Response Number 2
Name: Johnathan
Date: April 8, 2005 at 20:46:20 Pacific
Reply:

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/johnsite

I hope this helps. -Johnathan

PS: It would be funny if we were in the same class!


0

Response Number 3
Name: adriantheo
Date: April 10, 2005 at 17:54:38 Pacific
Reply:

what are u going on about johnathan

adriantheo


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


check date before removin... Indirect pointers in VB.N...



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: loop to calculate # of prime factor

Java (Prime factors) www.computing.net/answers/programming/java-prime-factors/4018.html

Help! Test if a number is Prime in C++ www.computing.net/answers/programming/help-test-if-a-number-is-prime-in-c/2794.html

using for loops to interate unicode www.computing.net/answers/programming/using-for-loops-to-interate-unicode/12858.html