Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 6379 |

@blackshadows | 6329 |

@mb1973 | 5358 |

@Quandray | 5231 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 4510 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3522 |

@sushant_a | 3459 |

@verma_ji | 3357 |

@KshamaGupta | 3318 |

Complete Leaderboard | |

Handle | Score |

@ritiksethi21 | 1050 |

@aroranayan999 | 807 |

@RizulBansal | 685 |

@ashishtrehan002 | 538 |

@hemantgarg923 | 528 |

@simrangoyal | 526 |

@ronaldo77 | 520 |

@anishrajan | 505 |

@thanosagain | 505 |

@ssparteek470 | 495 |

@rahul2312 | 491 |

Complete Leaderboard |

Given a number N, find least prime factors for all numbers from 1 to N. The least prime factor of an integer N is the smallest prime number that divides it.

**Note : **The least prime factor of all even numbers is 2. A prime number is its own least prime factor (as well as its own greatest prime factor).1 needs to be printed for 1.â€‹

**Example 1:**

**Input**: N = 6
**Output:** [1, 2, 3, 2, 5, 2]
**Explanation**: least prime factor of 1 = 1,
least prime factor of 2 = 2,
least prime factor of 3 = 3,
least prime factor of 4 = 2,
least prime factor of 5 = 5,
least prime factor of 6 = 2.
So answer is[1, 2, 3, 2, 5, 2].

**Example 2:**

**Input: **N = 4
**Output: **[1, 2, 3, 2]
**Explanation**: least prime factor of 1 = 1,
least prime factor of 2 = 2,
least prime factor of 3 = 3,
least prime factor of 4 = 2.
So answer is[1, 2, 3, 2].

**Your Task: **

You dont need to read input or print anything. Complete the function **leastPrimeFactor() **which takes N as input parameter and returns a list of integers containing all the least prime factor of each numbers from 1 to N.

**Expected Time Complexity:** O(NlogN)

**Expected Auxiliary Space:** O(N)

**Constraints:**

2<= n <=10^{3}

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Least Prime Factor

...