Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Find the Largest Prime Factor

Basic Acceptance 73.38% Points 10.00

You are given a positive integer n greater than 1. Your task is to return its largest prime factor.

A prime factor is a prime number that divides n exactly. If n is itself prime, its largest prime factor is the number itself.

Examples
Example 1

Input: n = 60
Output: 5
Explanation: The prime factors of 60 are 2, 3, and 5. The largest prime factor is 5.

Example 2

Input: n = 13
Output: 13
Explanation: Since 13 is prime, its only prime factor is 13 itself.

Example 3

Input: n = 100
Output: 5
Explanation: The prime factorization of 100 is 2 × 2 × 5 × 5, so the largest prime factor is 5.

Constraints
  • 2 = n = 10¹²
Companies
Apple eBay ServiceNow
Topics
Data Structures Basics 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