Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Find All Numbers Having Exactly K Divisors

Basic Acceptance 77.99% Points 10.00

You are given two integers N and K. Your task is to print all numbers from 1 to N that have exactly K positive divisors, in ascending order and separated by spaces.

Examples
Example 1

Input: N = 10, K = 2
Output: 2 3 5 7
Explanation: The numbers 2, 3, 5, and 7 are prime numbers, each having exactly two positive divisors: 1 and itself.

Example 2

Input: N = 20, K = 3
Output: 4 9
Explanation: The positive divisors of 4 are 1, 2, and 4. The positive divisors of 9 are 1, 3, and 9. Both numbers have exactly three positive divisors.

Example 3

Input: N = 5, K = 1
Output: 1
Explanation: The number 1 has exactly one positive divisor: 1. No other number from 1 to 5 has exactly one positive divisor.

Constraints
  • 1 = N = 105
  • 1 = K = 100
Companies
Apple eBay
Topics
Data Structures Basics Searching Algorithms Basics
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions