Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# Least prime factor of numbers till n

// C# program to print the least prime factors
// of numbers less than or equal to
// n using modified Sieve of Eratosthenes
using System;
 
class GFG
{
    public static void leastPrimeFactor(int n)
    {
        // Create a vector to store least primes.
        // Initialize all entries as 0.
        int []least_prime = new int[n+1];
 
        // We need to print 1 for 1.
        least_prime[1] = 1;
 
        for (int 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 (int 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 (int i = 1; i <= n; i++)
            Console.WriteLine("Least Prime factor of " +
                               i + ": " + least_prime[i]);
    }
     
    // Driver code
    public static void Main ()
    {
        int n = 10;
         
        // Function calling
        leastPrimeFactor(n);
    }
}
 
// This code is contributed by Nitin Mittal
Comment

PREVIOUS NEXT
Code Example
Csharp :: Max upload size for ASP.MVC CORE website 
Csharp :: user (current login user) in viewcomponent 
Csharp :: windows forms change double buffer during runtime 
Csharp :: c# accept any enum 
Csharp :: camera is rendering black screen unity 
Csharp :: how to refresh the data table in C# window form datagridview 
Csharp :: save and query mongodb collection as dynamic ExpandoObject 
Csharp :: c# XmlElement from string 
Csharp :: how to input message ox in c# 
Csharp :: loop code for X seconds 
Csharp :: how to populate a collection c# 
Csharp :: blazor wasm roles not working 
Csharp :: 2d collision handling jump table 
Csharp :: c# bitwise and 
Csharp :: c# run program as an administrator 
Csharp :: how to show enum name list from input in swagger c# 
Csharp :: c# ? behind variable 
Csharp :: best unity regex for email validation in c# 
Csharp :: uncapitalize string c# 
Csharp :: C# how to search textfile and append 
Csharp :: c# short 
Csharp :: c# inject dll into process 
Csharp :: unity 2d top down movement script 
Csharp :: world space constant size 
Csharp :: DateTime2 error in EF Blazor Web Application 
Csharp :: c# deeply related children 
Csharp :: f# list map function 
Csharp :: Using a Views in .net and c# 
Csharp :: for loop cs 
Csharp :: c# create empty file if not exists 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =