Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. I check, compile and run every program. Contact me at: aviboots(AT)netvision.net.il

Hostgator - Save 50% Off Your New Optimized WordPress Hosting With Coupon: WDA50

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Dedicated Hosting Superb Internet

simplilearn big data and analytics

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Buy Cool Shirts

getflywheel wordpress hosting


Distribute your Resume to 500+ Job Boards & Recruiters


6,300 questions

8,938 answers

573 users

How to print the prime factors of a number in PHP

Directory of Wholesale Companies and Dropship Suppliers | SaleHoo

4 views
asked Sep 4 by avibootz

1 Answer

0 votes
function get_prime_factors($number)
{
    if ($number <= 3)
        $factors = array($number);
    else
    {
        while ($number > 1)
        {
            for ($i = 2; $i < $number && $number % $i; $i++);
            
            $number /= $i;
            $factors[] = $i;
        }
    }
    return $factors;
}

for ($i = 0; $i <= 50; $i++)
    echo $i . ' = ' . implode(' * ', get_prime_factors($i)) . '<br />';      
 

 
/*
run: 
  
0 = 0
1 = 1
2 = 2
3 = 3
4 = 2 * 2
5 = 5
6 = 2 * 3
7 = 7
8 = 2 * 2 * 2
9 = 3 * 3
10 = 2 * 5
11 = 11
12 = 2 * 2 * 3
13 = 13
14 = 2 * 7
15 = 3 * 5
16 = 2 * 2 * 2 * 2
17 = 17
18 = 2 * 3 * 3
19 = 19
20 = 2 * 2 * 5
21 = 3 * 7
22 = 2 * 11
23 = 23
24 = 2 * 2 * 2 * 3
25 = 5 * 5
26 = 2 * 13
27 = 3 * 3 * 3
28 = 2 * 2 * 7
29 = 29
30 = 2 * 3 * 5
31 = 31
32 = 2 * 2 * 2 * 2 * 2
33 = 3 * 11
34 = 2 * 17
35 = 5 * 7
36 = 2 * 2 * 3 * 3
37 = 37
38 = 2 * 19
39 = 3 * 13
40 = 2 * 2 * 2 * 5
41 = 41
42 = 2 * 3 * 7
43 = 43
44 = 2 * 2 * 11
45 = 3 * 3 * 5
46 = 2 * 23
47 = 47
48 = 2 * 2 * 2 * 2 * 3
49 = 7 * 7
50 = 2 * 5 * 5
  
*/ 

 




answered Sep 4 by avibootz
...