Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php Least prime factor of numbers till n

<?php
// PHP program to print the
// least prime factors of
// numbers less than or equal
// to n using modified Sieve
// of Eratosthenes
 
function leastPrimeFactor($n)
{
    // Create a vector to
    // store least primes.
    // Initialize all entries
    // as 0.
    $least_prime = array($n + 1);
     
    for ($i = 0;
         $i <= $n; $i++)
    $least_prime[$i] = 0;
     
    // We need to
    // print 1 for 1.
    $least_prime[1] = 1;
 
    for ($i = 2; $i <= $n; $i++)
    {
        // least_prime[i] == 0
        // means it i is prime
        if ($least_prime[$i] == 0)
        {
            // marking the prime
            // number as its own lpf
            $least_prime[$i] = $i;
 
            // mark it as a divisor
            // for all its multiples
            // if not already marked
            for ($j = $i * $i;
                 $j <= $n; $j += $i)
                if ($least_prime[$j] == 0)
                $least_prime[$j] = $i;
        }
    }
 
    // print least prime
    // factor of numbers
    // till n
    for ($i = 1; $i <= $n; $i++)
        echo "Least Prime factor of " .
                            $i . ": " .
               $least_prime[$i] . "
";
}
 
// Driver Code
$n = 10;
leastPrimeFactor($n);
 
// This code is contributed
// by Sam007
?>
Comment

PREVIOUS NEXT
Code Example
Php :: php upload image to another subdomain 
Php :: create newfilter wordpress 
Php :: PHP strtr — Translate characters or replace substrings 
Php :: Including ACF in a custom theme or plugin 
Php :: get header sent var 
Php :: avoid grouping databases in phpmyadmin 
Php :: google calendar api push notifications php 
Php :: php echo variable name 
Php :: setUp() must be compatible with IlluminateFoundationTestingTestCase::setUp() 
Php :: replace class 
Php :: remove all breadcrumbs php 
Php :: HP officejet pro 8720 default password 
Php :: storefront header cart 
Php :: implode (PHP 4, PHP 5, PHP 7, PHP 8) implode — Join array elements with a string 
Php :: How to prevent repeating the same option value of a selection in a php loop 
Php :: syntax error, unexpected variable "$result" in D:wordpressxampphtdocs empleteuser_delete.php on line 13 
Php :: br2nl 
Php :: file upload yii2 rest api 
Php :: php linkify text 
Php :: php division without round 
Php :: fichiers en php 
Php :: How to Filter Your Posts & Pages by Custom Field in WordPress Dashboard 
Php :: laravel media library regenerate 
Php :: php printf percent sign 
Php :: laravel ignition dark mode 
Php :: how to check my server use cgi, fcgi or fpm. 
Php :: php if form fails keep data 
Php :: how to react on a html button click in php 
Php :: detect change in log file in real time php 
Php :: Number in English Words (Indian format) php 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =