Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java Least prime factor of numbers till n

// Java program to print the least prime factors
// of numbers less than or equal to
// n using modified Sieve of Eratosthenes
 
import java.io.*;
import java.util.*;
 
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++)
            System.out.println("Least Prime factor of " +
                               + i + ": " + least_prime[i]);
    }
    public static void main (String[] args)
    {
        int n = 10;
        leastPrimeFactor(n);
    }
}
 
// Code Contributed by Mohit Gupta_OMG <(0_o)>
Comment

PREVIOUS NEXT
Code Example
Java :: java awt bild einfügen 
Java :: how to remove all components from layeredPane java 
Java :: managa firebase users 
Java :: pack in swing 
Java :: java GLIBC 2 34 missing 
Java :: java compareto jdei stackoverflow 
Java :: Get Subarray from Array 
Java :: ejercicios de clases abstractas e interfaces en java 
Java :: how to add a singleton hashset in java 
Java :: size of a tree node java linked;ist 
Java :: convert boolean to Boolean class 
Java :: setlist arraylist java swing example 
Java :: konva crop outside width and height of image 
Java :: banner generator spring boot 
Java :: public static void nPrintln(String message, int n) { 
Java :: how is boolean array initialized if no input is given java 
Java :: Java Protected Access Modifier package two 
Java :: gradle use local path 
Java :: what is xml file in java 
Java :: Java Throwing checked exception 
Java :: capitalize a letter in java 
Java :: transpose array in java 
Java :: replace string placeholder java 
Java :: public CustomAdapter(Context context, ArrayList<HashMap data, int resource, String[] from, int[] to) 
Java :: java multi threading Buzzer program 
Java :: has places api got deprecated??? 
Java :: firemonkey android ini file 
Java :: site:stackoverflow.com List is abstract; cannot be instantiated public List<Integer result = new List<(); 
Java :: get string match percentage java 
Java :: java method 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =