Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript Least prime factor of numbers till n

<script>
// javascript 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.
    let least_prime = Array(n+1).fill(0);
 
    // We need to print 1 for 1.
    least_prime[1] = 1;
 
    for (let 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 (let j = i*i; j <= n; j += i)
                if (least_prime[j] == 0)
                   least_prime[j] = i;
        }
    }
 
    // print least prime factor of
    // of numbers till n
    for (let i = 1; i <= n; i++)
       document.write( "Least Prime factor of "
             + i + ": " + least_prime[i] + "<br/>");
}
 
// Driver program to test above function
    let n = 10;
    leastPrimeFactor(n);
     
// This code is contributed by Rajput-Ji
 
</script>
Comment

PREVIOUS NEXT
Code Example
Javascript :: react axios POST with super constructor parent class 
Javascript :: middleware for angular for passing token in header 
Javascript :: calculate avg count from month in year js 
Javascript :: ajaxpost 
Javascript :: CDNs for arquero 
Javascript :: regex to allow spaces and characters 
Javascript :: react email validation 
Javascript :: how to filter through an array of objects 
Javascript :: save slug on schema pre save in node js 
Javascript :: TOP Array Methods 
Javascript :: unexpected text node: . a text node cannot be a child of a <view. 
Javascript :: upload file to s3 using pre signed url javascript 
Javascript :: hide Card Number Format 
Javascript :: routing with django and react 
Javascript :: aysnc and await response data usage 
Javascript :: prisma.db firebase 
Javascript :: sort an array in descending order javascript 
Javascript :: do while 
Javascript :: menu with dynamic submenu in javascript 
Javascript :: v-smooth-scroll 
Javascript :: set timeout with no name 
Javascript :: function multiply(a b) a * b javascript 
Javascript :: get time in between two dates javascript dayjs 
Javascript :: how to put condition on pagination material table 
Javascript :: Laravel summernote HTML output 
Javascript :: jsf localdate converter 
Javascript :: count object based on status and shop using javascript 
Javascript :: Angularjs to Angular Migration: factory prototype 
Javascript :: Relaxed "angularjs" style expression parsing missing in vue 
Javascript :: socket io check send 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =