Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function to return prime factors

Status
Not open for further replies.

748323

Programmer
Dec 10, 2004
70
US
Grr.. I've been trying to figure this out for a few days now, but I can't figure it out. I've been into PHP for about 6 months now, so I know the functions needed. I just can't convert it into code.

Here is a function I made to return the first pair of factors that isn't 1 or the number. 20 would return 2-10

Code:
<?

function firstfactor($num) {
	for ($i=2; $i <= $num/2; $i++) {
		if (($num/$i) == floor($num/2)) {
			return $i . '-' . $num/$i;
		}
	}
}
?>

I also made a function to check if a number is prime, a decimal, and a function that returns all factors.

Can you help me out a bit? Please, no PHP code. I would rather just have a hint pointing me in the right direction mentally. Thanks!
 
Wait. Let me try this. I know I can do it. I'll run back here if I can't do it by tonight.
 
Err.. I can't do it. I tried, but can't. Help is welcomed. Thanks.
 
wotcha - what's the actual problem? that it's not returning what you want?
 
i see. it doesn't work for odd numbers!

without providing php code, and sticking with the logic of your code, surely once $i is fully incremented and no result is found, you need to increment the divisor of $num and restart the if loop. reductor ad finitum if no answer you have a prime.

hth
Justin
 
I need to do this by myself. I'll keep trying. Its just going to click one day.
 
are you going to post the result. so far this post is pretty much an existentialist monologue!
 
Err... jpadie you are right. To be or not to be...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top