Thread: Mod limits.h? Function to check for prime #s? If not, could one create a .h to do so?

  1. #1
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80

    Mod limits.h? Function to check for prime #s? If not, could one create a .h to do so?

    A while back I created a program to check if a number was prime. Basically it checked to see if a number was divisible by any number between 2 and it's square root, if it returned true, the number wasn't prime, if it returned false, it would print it out to a file with a comma and a space ', '

    I don't mind redoing it in C (the original was done in C++) but I haven't made a .h file and I figured it would be a learning experience.

    I also heard about limits.h and I'd like to change the limit--temporarily--to one centillion (a number with 100 '0s' after it) to see how high I can compute prime numbers. As I think I'd need to do this to calculate any prime number above 2,147,483,647 (or below -2,147,483,647 for that matter).

    The algorithm I don't really need help with, mostly just the .h files and any info on creating a function and modifying 'limits.h' to suit my purposes.
    If, for some reason, I don't make any sense at all, it's highly likely that I've been up all night on some strange bender that isn't normal. I likely unarmed and far from dangerous. While I haven't been known to physically harm anyone or anything else, there have been unsubstantiated reports that I have driven others into both a hazy stupor as well as a blinding rage. Otherwise, I hope I helped.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    After you modify limits.h, feel free to speed the next few months of your life re-writing the rest of the C library so it works with your modified limits.h.

    Hint: Get BigNum or related library and use it.
    Arbitrary-precision arithmetic - Wikipedia, the free encyclopedia

    Tim S.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    First modifying a standard header is not recommended. Plus just "modifying" the header will not allow you to use a type with a larger value. These limits are the limits of the compiler. You would be better off investigating something like: GNU Multiple Precision Library .

    Jim

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Generally, you can create your own .h file and add your own #defines. Never modify system header files unless you've got a really good reason.

    For your case, you simply cannot calculate numbers that high, as (assuming you have a newer computer) 2^64 is the highest value you can store (uint64_t). This only adds up to about 20 digits, only a fifth of what you want. If you really need anything higher, you're gonna need a library.

    edit: ninja'd by jim and stahta

  5. #5
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    I have 6 gb of RAM and an Intel quad core 2.5ghz processor... that change anything (I don't think so, but I don't mind asking)? I can take only being able to calculate prime numbers up to 18,446,744,073,709,551,616 with a grain of salt and deal with it :P.

    Jim, I'm going to look into that, as the one that Tim recommended seems to only be available in C# and .NET
    If, for some reason, I don't make any sense at all, it's highly likely that I've been up all night on some strange bender that isn't normal. I likely unarmed and far from dangerous. While I haven't been known to physically harm anyone or anything else, there have been unsubstantiated reports that I have driven others into both a hazy stupor as well as a blinding rage. Otherwise, I hope I helped.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Quote Originally Posted by andrew89 View Post
    I can take only being able to calculate prime numbers up to 18,446,744,073,709,551,616 with a grain of salt and deal with it :P.

    Jim, I'm going to look into that, as the one that Tim recommended seems to only be available in C# and .NET
    If you only want to go up until that number, you don't need a library, just use the data type uint64_t whenever making calculations.

    Quote Originally Posted by andrew89 View Post
    I have 6 gb of RAM and an Intel quad core 2.5ghz processor... that change anything
    Not anymore, the limits are pretty standardized now. The problem mainly affects really old computers.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I have 6 gb of RAM and an Intel quad core 2.5ghz processor... that change anything
    Nope. The limits defined in limits.h are the limits that the compiler can handle.

    Jim

  8. #8
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    So after installing GMP I'd use:
    Code:
    uint64_t primeNum;
    in order to declare something larger than what my compiler would normally be able to handle?
    If, for some reason, I don't make any sense at all, it's highly likely that I've been up all night on some strange bender that isn't normal. I likely unarmed and far from dangerous. While I haven't been known to physically harm anyone or anything else, there have been unsubstantiated reports that I have driven others into both a hazy stupor as well as a blinding rage. Otherwise, I hope I helped.

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Quote Originally Posted by andrew89 View Post
    So after installing GMP I'd use:
    Code:
    uint64_t primeNum;
    in order to declare something larger than what my compiler would normally be able to handle?
    No, you don't need to install GMP if you're only using uint64_t.

    GMP is for numbers longer than ~20 digits, anything shorter is natively supported by C.

    EDIT: make sure you include <stdint.h> or it won't necessarily work.

  10. #10
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    I want to go as high as I possibly can, and GMP is installed now. So how would I go about going higher with GMP?

    Edit: In case anyone wanted to know why, after all, this is completely unpractical. My answer is simple. Because I can. Or at least I hope I can...
    Last edited by andrew89; 12-27-2011 at 11:05 AM. Reason: Additional information.
    If, for some reason, I don't make any sense at all, it's highly likely that I've been up all night on some strange bender that isn't normal. I likely unarmed and far from dangerous. While I haven't been known to physically harm anyone or anything else, there have been unsubstantiated reports that I have driven others into both a hazy stupor as well as a blinding rage. Otherwise, I hope I helped.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by andrew89 View Post
    A while back I created a program to check if a number was prime. Basically it checked to see if a number was divisible by any number between 2 and it's square root, if it returned true, the number wasn't prime, if it returned false, it would print it out to a file with a comma and a space ', '

    I don't mind redoing it in C (the original was done in C++) but I haven't made a .h file and I figured it would be a learning experience.

    I also heard about limits.h and I'd like to change the limit--temporarily--to one centillion (a number with 100 '0s' after it) to see how high I can compute prime numbers. As I think I'd need to do this to calculate any prime number above 2,147,483,647 (or below -2,147,483,647 for that matter).

    The algorithm I don't really need help with, mostly just the .h files and any info on creating a function and modifying 'limits.h' to suit my purposes.
    The values in limits.h are determined by
    A) The design of your compiler
    B) The size of your CPU's registers

    Most compilers will support 64 bit integers (long long int, int64_t) ONLY if the underlying hardware supports it.

    the limits.h file is only a set of constants that *reflect* the limits of the compiler... they do not *set* them, that's pretty much hard wired into the code.

    The flatout last thing you want to do is take a stable compiler system (code, libs, headers) and start messing with it... Well, unless you want *every* program you write to contain unfixable errors, that is.

  12. #12
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    Quote Originally Posted by CommonTater View Post
    *snip*
    The flatout last thing you want to do is take a stable compiler system (code, libs, headers) and start messing with it... Well, unless you want *every* program you write to contain unfixable errors, that is.
    I understand that first part now, thank you though.

    I'm likely not going to keep GMP installed after I write, compile, and run this program once to generate the file. Afterwards I might remove GMP if it causes any problems.

    I just want to see if I can do it.
    If, for some reason, I don't make any sense at all, it's highly likely that I've been up all night on some strange bender that isn't normal. I likely unarmed and far from dangerous. While I haven't been known to physically harm anyone or anything else, there have been unsubstantiated reports that I have driven others into both a hazy stupor as well as a blinding rage. Otherwise, I hope I helped.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by andrew89
    So how would I go about going higher with GMP?
    By learning how to use the GMP integer interface
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    Quote Originally Posted by laserlight View Post
    By learning how to use the GMP integer interface
    Thank you I'll be reading this for a while if anyone needs me.

    Also, in case anyone was curious, I found the limit.

    /* Minimum and maximum values a `signed long long int' can hold. */
    # define LLONG_MAX 9223372036854775807LL

    So... 9,223,372,036,854,775,807 is the max? Until I get GMP running anyway?
    If, for some reason, I don't make any sense at all, it's highly likely that I've been up all night on some strange bender that isn't normal. I likely unarmed and far from dangerous. While I haven't been known to physically harm anyone or anything else, there have been unsubstantiated reports that I have driven others into both a hazy stupor as well as a blinding rage. Otherwise, I hope I helped.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-29-2010, 10:34 AM
  2. Check wheather the entered number is prime also repeat it
    By rajesh10071986 in forum C Programming
    Replies: 6
    Last Post: 01-11-2010, 04:34 AM
  3. Prime Number Function
    By mrcg in forum C Programming
    Replies: 10
    Last Post: 10-21-2009, 12:33 AM
  4. Prime number check
    By password in forum C++ Programming
    Replies: 3
    Last Post: 06-25-2007, 10:30 AM
  5. How is to check prime number?
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 10-04-2001, 11:36 PM