Largest Prime Factor Problem Statement

You are given a positive integer n. Your task is to identify the largest prime factor of this given positive integer.

If there is no prime factor for a given integer, output -1.

Input:

The first line of input contains an integer T representing the number of test cases. The subsequent T lines each contain a positive integer n.

Output:

For each test case, output the largest prime factor of the given positive integer, on a separate line.

Example:

Input:
2
10
17
Output:
5
17
Explanation:
  • For n = 10, the prime factors are 2 and 5, thus largest is 5.
  • For n = 17, since 17 is a prime number itself, 17 is the largest prime factor.

Constraints:

  • 1 ≤ T ≤ 50
  • 1 ≤ n ≤ 10^9
AnswerBot
4mo

Identify the largest prime factor of a given positive integer.

  • Iterate from 2 to sqrt(n) to find prime factors

  • Check if each factor is prime and update largest prime factor

  • If no prime factor found, outp...read more

Help your peers!
Select
Add answer anonymously...

Times Internet Software Developer interview questions & answers

A Software Developer was asked Q. Write a function to delete a node in a singly linked list, given only access to ...read more
A Software Developer was asked Q. Which data structure would you use to program a jigsaw puzzle program, and what ...read more
A Software Developer was asked Q. What is polymorphism? Can you provide some examples?

Popular interview questions of Software Developer

A Software Developer was asked Q1. Write a function to delete a node in a singly linked list, given only access to ...read more
A Software Developer was asked Q2. Which data structure would you use to program a jigsaw puzzle program, and what ...read more
A Software Developer was asked Q3. What is polymorphism? Can you provide some examples?
Times Internet Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits