Jump to content


Register a free account to unlock additional features at BleepingComputer.com
Welcome to 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.

Photo

Stack Overflow (I think)


  • Please log in to reply
7 replies to this topic

#1 comet@earth

comet@earth

  • Members
  • 170 posts
  • OFFLINE
  •  
  • Local time:02:51 PM

Posted 14 September 2011 - 07:23 AM

Kindly tell me why the program below stalls for a 12-digit number
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

BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Moderator
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:04:21 AM

Posted 14 September 2011 - 06:57 PM

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".

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


#3 comet@earth

comet@earth
  • Topic Starter

  • Members
  • 170 posts
  • OFFLINE
  •  
  • Local time:02:51 PM

Posted 15 September 2011 - 11:52 AM

I don't know what really happens.I just enter the number and the cursor moves to the next line and keeps blinking.I have to use "ctrl+c" to end the program.Could you suggest me a solution.This is a problem from Project Euler.
Thank You

#4 groovicus

groovicus

  • Moderator
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:04:21 AM

Posted 15 September 2011 - 12:18 PM

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.

#5 varunit

varunit

  • Members
  • 7 posts
  • OFFLINE
  •  
  • Local time:02:51 PM

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

#6 comet@earth

comet@earth
  • Topic Starter

  • Members
  • 170 posts
  • OFFLINE
  •  
  • Local time:02:51 PM

Posted 26 November 2012 - 12:14 AM

Thnaks

#7 AceInfinity

AceInfinity

  • Members
  • 26 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Canada
  • Local time:03:21 AM

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.

mvp.png
Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer


#8 groovicus

groovicus

  • Moderator
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:04:21 AM

Posted 14 January 2013 - 06:55 PM

In most cases, yes, I would agree with you. In this case, comet@earth is pretty much self taught and does not have the benefit of more rigorous experience.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users