Write a Java program to list out the first 1500 natural numbers whose factors are either ONLY 2, 3, or 5.

a-java-program-to-find-natural-number-thoes-factors-are-2,3-and-5-only

Hi, Welcome to the Practice House. Yet another post about a common java interview question. I got this question from a recruiter and the challenge was the program need to run in less than 2 minutes.

Below I have shared my solution for this particular question. it was successfully run within 57 Seconds.

Java Interview Question

Okay, the Question is Write a Java program to list out the first 1500 natural numbers whose factor(s) is/are either ONLY 2, 3, or 5. The faster your program can complete the calculation the better, but it should not run more than 2 minutes.
For your reference, the first 10 numbers are (factors listed after semicolon):

PS: 21 (3*7), 22 (2*11), 26 (2*13) are numbers that should not be in the list.

So this is our condition that we need to find out the number of those factors are only 2,3, and 5. we can’t have the number if their factors are 2,3 and 5 but got 7 also. only if it’s 3 we can take.

Please try it out. Maybe you can find a better way to do it. And if you are able to find the better way please share with us in the comments section below.

Clone / Download the Sample Project from GitHub : Download / Clone Sample Project

 

Sample Code to find natural numbers whose factor(s) is/are either ONLY 2, 3, or 5 :

Sample Output for the program:

1500 Natural Numbers output

Thanks for visiting the practicehouse. If you have any suggestions or advice or question please let us know. we would love to help you. If you think this post helped you please share this knowledge with your friends and visit us again.

Thank you. See you Soon In any other post.

Leave a Reply

avatar
  Subscribe  
Notify of