largest number prime number that can be produced...

This is a discussion on largest number prime number that can be produced... within the C Programming forums, part of the General Programming Boards category; my code:: can anyone suggest how to make the output larger????? Code: #include<stdio.h> #include<limits.h> #include<float.h> int main() { unsigned int ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    73

    largest number prime number that can be produced...

    my code::

    can anyone suggest how to make the output larger?????

    Code:
    #include<stdio.h>
    #include<limits.h>
    #include<float.h>
    int main()
    {
    	unsigned int num,tmp,suc=1,i;
    	num=UINT_MAX;
    	do
    	{
    		suc=1;
    		tmp=num;
    		for(i=2;i<=tmp/2;i++)
    		{
    			if(tmp%i==0)
    			{
    				suc=0;
    				break;
    			}
    		}
    		num--;
    	}while(suc==0);
    	printf("%u",num+1);
    	getchar();
    	return 0;
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    what do you mean by lager?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    73
    larger means the biggest prime number possible

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    you can try
    unsigned long
    unsigned long long (not supported by all compilers)
    some library for working with extreamly long numbers
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by ElemenT.usha View Post
    larger means the biggest prime number possible
    http://www.prime-number.org
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,294
    "the biggest prime number possible" means nothing. There is no limit beyond which all numbers are composite. The only possible thing you can mean is the largest prime number known to man.
    You don't have a hope of getting anywhere remotely near the largest primes known to man using such an archaic and naieve algorithm. It would literally take Millennias (thousands of years)! No I'm not exagerating.

    Hell, I've written a decent bigint library, but even I gave up hope of doing realistically useful primality testing.
    Even getting into the upper realms of 64-bit territory just gets ridiculous with how long it takes using the algorithm you provided. Faster algorithms like your typical sieve of Eratostheses overflow conventional PC memory with this size too.

    I'm sorry, but unless you use a special purpose library, you simply wont be able to do what you want!

    You could try understanding the Primality testing algorithms in Wikipedia, but be warned, you'd best become a maths professor first.
    Last edited by iMalc; 02-16-2008 at 12:59 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    73
    actually i need to use a time function and find the largest possible in 2sec..
    so can you tell me some means to speed up my process

  8. #8
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    It could be possible for 64 bits but only with very advanced methods.
    As iMalc pointed out, do a little Wiki study, try implementing the suggested methods.
    It might turn out to be a difficult task, and you probably need to know good 'C' to get it to work and be correct also, even for someone who knows good 'C'.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,294
    Quote Originally Posted by ElemenT.usha View Post
    actually i need to use a time function and find the largest possible in 2sec..
    so can you tell me some means to speed up my process
    Well, how about telling us that to being with?
    So, how does that work? You need to explain furthur. Do you just start writing out primes, and after 2 seconds your program is killed off and the highest prime you outputted during that time is the winner?

    You can get some improvement by storing a list of primes as you go, and testing against primes in the list that are less than the square root of the number, instead of testing all odd numbers.
    Note that this is still significantly slower than the Sieve of Eratostheses, but past a certain point it requires less extra memory. I'm not going to help much beyond that because this is obviously a test of some kind.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem running prime number test.
    By galactic_ronin in forum C++ Programming
    Replies: 25
    Last Post: 02-20-2008, 11:21 AM
  2. Largest number program
    By rebel in forum C++ Programming
    Replies: 10
    Last Post: 12-01-2005, 03:20 AM
  3. Prime Number stops after 29, but why?
    By Daveo in forum C Programming
    Replies: 22
    Last Post: 09-17-2004, 10:55 AM
  4. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM
  5. How is to check prime number?
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 10-04-2001, 11:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21