I know how to validate input, but how can I check if
is an integer and not a float?Code:int ans = num1 / num2; // all integers
Printable View
I know how to validate input, but how can I check if
is an integer and not a float?Code:int ans = num1 / num2; // all integers
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.
Check if num1 is perfectly divisible by num2.
And check num2 isn't zero before doing the division.
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;
}
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.Quote:
But the max. integer value is 2147483647. Is there any way I can exceed that limit?
on some implementations, unsigned long long will give you a 64-bit int.
Quote:
Originally Posted by laserlight
GMP:
Hmm...Quote:
Originally Posted by http://www.swox.com/gmp/guestaccount.html
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.
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
}
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.
Thx for all the replies!
@ jafet:
I modified my source, everytime the program runs, it asks for a number where to begin searching.
I'll try your way too!
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:
checks if unsigned int i is even or odd. The program has become faster.Code:for(i; i < UINT_MAX; i++)
{
if(i % 2 > 0)
{
prime(i);
}
else if(i == 1)
{
continue;
}
}
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).
KurtQuote:
Originally Posted by Ideswa
You're right! I changed it.
Being limited to 32-bit numbers is only half of the problem.
Using the "Sieve of Eratosthenes" takes far too much memory when you get close to the maximum 32-bit int.
Using the method where you simply check for division with every number up to the square root takes no extra memory but is even slower. This bring me to my next point. This method is far too slow for numbers bigger than 32 bits can handle anyway.
Take a look at http://en.wikipedia.org/wiki/Primality_test
Or better yet, get libmp++-2.0.1d which implements large number types and the Rabin-Miller primality test.