# check value

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 03-15-2006
Ideswa
check value
I know how to validate input, but how can I check if

Code:

`int ans = num1 / num2; // all integers`
is an integer and not a float?
• 03-15-2006
Daved
It will be an integer no matter what, because integer division always results in an integer. To check to see if there would be any remainder, you could use the modulus operator %. Any non-zero result from num1 % num2 means there would be a remainder after the division.
• 03-15-2006
laserlight
Check if num1 is perfectly divisible by num2.
• 03-15-2006
Salem
And check num2 isn't zero before doing the division.
• 03-15-2006
Ideswa
Thx for the replies! I used it for a source code to calculate prime numbers. The user can input a number where to start searching, everytime the program encounters a prime number: it says "number" is a prime number. However, I want to search veeeeeeery big numbers.
But the max. integer value is 2147483647. Is there any way I can exceed that limit?

This is the source code:

Code:

```#include <iostream> #include <windows.h> using namespace std; void prime(int pm) {   system("cls");   int ans = 0;   bool prime = true;     if(pm == 1)   {     prime = false;   }     for(int i = 2; i < pm; i++)   {     if(pm % i > 0)     {       continue;     }     else     {       prime = false;       break;     }   }     if(prime)   {     cout << pm << " is a prime number!\n\n";     system("PAUSE");   } } int main() {   int i = 0;   cout << "insert start number (integer, cannot be larger than " << INT_MAX << "): ";   cin >> i;     while(i < 1 || i > INT_MAX)   {     cout << "Number cannot be smaller than 1 and cannot be larger than " << INT_MAX << "! Please re-enter: ";     cin.clear();     cin.ignore();     cin >> i;   }     for(i; i < INT_MAX; i++)   {     prime(i);   }   Sleep(5000);   return 0; }```
• 03-15-2006
laserlight
Quote:

But the max. integer value is 2147483647. Is there any way I can exceed that limit?
Use unsigned int, but then the max would probably be 4294967295 for you, which is still pretty small. You could give the GMP library a try.
• 03-16-2006
Bench82
on some implementations, unsigned long long will give you a 64-bit int.
• 03-18-2006
jafet
Quote:

Originally Posted by laserlight
You could give the GMP library a try.

GMP:

Quote:

Originally Posted by http://www.swox.com/gmp/guestaccount.html
People sometimes offer us accounts on Windows machines; unfortunately, that isn't useful since we don't develop GMP for such arcane platforms.

Hmm...

I also made a prime calculator once, but it stuck to the 2^32-1 limit. You can make your own simple class (struct if you use C) to handle bigger numbers, though.
• 03-18-2006
jafet
Your code is damn slow, it runs at O(n^2) (at best reduced to O(n^2 / log n) ) where n is the prime size, which is very very bad. You will never reach 2147483647 in any case within a week of running that, so don't bother.

The following code only tests divisions up to the number's square root, which is sufficient. It also uses primes calculated previously instead of dividing by every integer from 2 onwards. Another minor improvement is testing only odd numbers (after 2 that is).

This calculates a set number of primes, not primes up to a limit, but I'm assuming you can adapt this to your own needs ;)

This will perform 1,000s of times faster (no kidding!) than yours at high numbers, say primes up to 10,000,000.

Code:

```cout << "How many primes? "; cin >> num; //allocate enough memory unsigned int* primelist = new unsigned int[num]; //init first prime primelist[0] = 2; //number of primes int index = 1; //first test number unsigned int test = 3; //largest prime calculatable, used to determine limit unsigned int maxprime = 4294967291; //loop to get primes while(test <= maxprime && num > index) {         int i = 0;         //while each existing prime in list does not divide evenly into test number         //only tests up to square root of test number         while(test % primelist[i] && test >= primelist[i] * primelist[i])         {                 //go to next prime to test                 i++;         }         //if we've completed test above up to the square root, then test number is new prime         if(primelist[i] * primelist[i] > test)         {                 //add to number of primes index                 index++;                 //add number to primes list                 primelist[index] = test;         }         //only need to test odd numbers     test += 2;     //reset i to test new number }```
• 03-18-2006
OnionKnight
Installing a new library might be hassle for a beginner. You can always use a 64-bit integer by using whatever type(s) supported by your compiler which could be long long, int64, __int64 or something completely different. Unsigned 64-bit integers have a ceiling of 18446744073709551615 which is pretty neat. If that's not enough you could try a 128-bit int if your compiler has support for it.
• 03-18-2006
Ideswa
Thx for all the replies!

@ jafet:
I modified my source, everytime the program runs, it asks for a number where to begin searching.

• 03-18-2006
Ideswa
Your program crashes often! And mine goes faster, I started yours and 30 secs after it I started mine (from number 2). Mine cathed up with yours.

If I input large numbers your program crashes.

Edit:
When I input 6, your program crashes too. And it skippes 2, but 2 definitly is a primenumber!

Edit2:
I improved my program:
Code:

```for(i; i < UINT_MAX; i++)   {     if(i % 2 > 0)     {       prime(i);     }     else if(i == 1)     {       continue;     }   }```
checks if unsigned int i is even or odd. The program has become faster.
• 03-18-2006
laserlight
jafet, the GMP library can be used on MS Windows, just that it isnt developed on MS Windows. That said, OnionKnight is right in that installing it may not be that easy (but there's a devpak at devpaks.org, I think. Could always upload my own if needed, but then compiling the library may be better than relying on a devpak).
• 03-18-2006
ZuK
Quote:

Originally Posted by Ideswa
Code:

```for(i; i < UINT_MAX; i++)   {     if(i % 2 > 0)     {       prime(i);     }     else if(i == 1)  // this test just slows things down. the test will always be false     {       continue;     }   }```

Kurt
• 03-18-2006
Ideswa
You're right! I changed it.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last