Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Count Distinct Prime Factors

Basic Acceptance 87.32% Points 10.00

You are given a positive integer n. Your task is to return the number of distinct prime factors of n. A prime factor is counted only once, even if it divides n multiple times.

Examples
Example 1

Input: n = 60
Output: 3
Explanation: The distinct prime factors of 60 are 2, 3, and 5. Therefore, the count is 3.

Example 2

Input: n = 8
Output: 1
Explanation: Since 8 = 2³, its only distinct prime factor is 2. Therefore, the count is 1.

Example 3

Input: n = 1
Output: 0
Explanation: The number 1 has no prime factors, so the count is 0.

Constraints
  • 1 = n = 10¹²
Companies
Microsoft eBay Mastercard
Topics
Data Structures Basics Hashing 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