import java.io.*; import java.util.Scanner; class primefactor { public static void main(String args[]) { long num,i,j,count=0L,rem; long ans; System.out.println("Enter the number whose greatest prime factor is required:"); Scanner sc=new Scanner(System.in); num=sc.nextLong(); for(i=1;i<=num;i++) { for(j=1;j<=i;j++) { if(i%j==0) count=count+1; } if(count==2) { rem=num%i; if(rem==0) ans=i; } count=0; } System.out.println("The answer is "+ ans); } }Thank You

**BleepingComputer**, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having

**no ads**shown anywhere on the site.

**Click here to Register a free account now!**or read our

**Welcome Guide**to learn how to use this site.

# Stack Overflow (I think)

### #1

Posted 14 September 2011 - 07:23 AM

### BC AdBot (Login to Remove)

### #2

Posted 14 September 2011 - 06:57 PM

EDIT: What do you mean by 'stalls'? That could mean anything from "it pauses for 5 seconds" or "the application freezes and I have to reboot my computer".

**Edited by groovicus, 15 September 2011 - 07:28 AM.**

### #3

Posted 15 September 2011 - 11:52 AM

Thank You

### #4

Posted 15 September 2011 - 12:18 PM

And it takes a really long time to calculate large primes. Calculating the largest prime factor for a 6 digit number using your algorithm takes about 30 seconds, and will get exponentially longer. Fix your programming error and let the program run for a couple of hours.

### #5

Posted 28 September 2011 - 10:26 AM

You have programming errors. You have variables that have not been initialized.

And it takes a really long time to calculate large primes. Calculating the largest prime factor for a 6 digit number using your algorithm takes about 30 seconds, and will get exponentially longer. Fix your programming error and let the program run for a couple of hours.

You can generate prime numbers for larger digit numbers using efficient algorithms, which would give you the answer within seconds or minutes..

Here is one called as Sieve

Sieve_of_Eratosthenes

### #7

Posted 14 January 2013 - 09:00 AM

Because it takes a really long time to calculate large prime numbers??

EDIT: What do you mean by 'stalls'? That could mean anything from "it pauses for 5 seconds" or "the application freezes and I have to reboot my computer".

Not exactly with an efficient algorithm. I wrote a method in C# that will calculate whether a 12 digit number is prime in less than a even a small fraction of a second, and a 19 digit number in ~10-15 seconds. Sieve of Eratosthenes is a good method though too. I re-wrote a method using it and was able to get faster times. I wasn't aware of it, I came up with my own method that uses part of that methodology though.

**Edited by AceInfinity, 14 January 2013 - 09:07 AM.**

Microsoft MVP .NET Programming - (2012 - Present)

®Crestron DMC-T Certified Automation Programmer

### #8

Posted 14 January 2013 - 06:55 PM

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users