473,396 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Program to Test Large Number for Prime

For a fun challenge, I am trying to create a C++ program that will determine if a number is prime. I am getting to problems when I reach a large values (over 9 or so digits).
How can I increase the number of digits i can use?
For example, when i use the number 4362206209 (which I know for a fact is prime) it returns 2 as a factor, which it isn't.
Eventually, I would like to get to long (50+ digit) numbers. I know this calculation would take a long time, but i have several (mathematical) tricks for cutting down on the numbers tried... if I could only get the computer do divide it for me.

Here's my code for the simplist program:

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<cmath>
  3.  
  4. using namespace std;
  5. int main (){
  6.     unsigned long int primeNumber, testNumber =1, testResult = 1;
  7.     cin>>primeNumber;
  8.     bool prime=false;
  9.     while (!prime & testNumber <= sqrt(primeNumber)){
  10.         testNumber++;
  11.         testResult = primeNumber % testNumber;
  12.         if (testResult == 0){
  13.             prime = true;
  14.         }
  15.     }
  16.     if(prime){
  17.         cout<<"first factor is : "<<testNumber;
  18.     }
  19.     else{
  20.         cout<<"it's a prime number";
  21.     }
  22.     return 0;
  23. }
  24.  
Thank you
Oct 1 '07 #1
6 5257
Ganon11
3,652 Expert 2GB
Your large numbers are not working properly because they are larger than what an unsigned long can hold (its max value is 4,294,967,296). In order to alleviate this, you could possibly evaluate these numbers by their binary equivalent using a bitset? Maybe? I have never actually used this, so I'm not sure how well it will work.
Oct 1 '07 #2
RRick
463 Expert 256MB
A long int is the same size as an int on many systems. In this case, your have run into a number greater than the 32 bit integer resolution on your system.

Try using unsigned long long int instead. On some systems this will create 64 bit integers, which is good for integers up to 18446744073709551616ULL.
Oct 1 '07 #3
Thanks, but is there a way I can put very large numbers in? I can put a lage (50+ digit) number into an array, but I'm not sure how i could handle division in this case.
I am particularly interested in the RSA- Factoring Challenge where the numbers can be as long as 600+ digits.

http://en.wikipedia.org/wiki/RSA_Factoring_Challenge
Any advice?

I tried the unsigned long long int and that worked, but I wanted to use larger numbers.
Thanks
Oct 2 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
I tried the unsigned long long int and that worked, but I wanted to use larger numbers.
Thanks
You will need to use arrays probably as a struct member and various functions for math operations on the arrays.
Oct 2 '07 #5
You will need to use arrays probably as a struct member and various functions for math operations on the arrays.
Could you perhaps provide a (simple) example to help me get started?

Thank you
Oct 3 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
You write a class that contains a string.

There should be member for the sign.

There should be a member for the location of the decmimal point.

To Add, you position to the least signifcant character and add the values. If greater than 9, you have a carry, etc.. You will work this out like you were doing it on paper. Most likely this will be the operator+ method.

I shouldn't be too hard.
Oct 3 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: takashi | last post by:
Hi, I have a question. I am learning about how to use c++ language. I have attempted to make my own programs, using the knowledge that I have, but sometimes when I get stuck on writing a code, it...
7
by: brian.digipimp | last post by:
Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is 2. An odd...
2
by: wudoug119 | last post by:
This is my code and it will take any number that I input and say it is a prime number. Please help me... int Prime(int prime) //declares isPrime as a function using integers { ...
3
by: smayon | last post by:
Here's the problem- Write a C++ program that prompts the user to input a positive integer then output the number and a message indicating whether the number is a prime number. I found this...
2
by: QHorizon | last post by:
Hello, I'm new to Python (I've learned everything up to iterators so far) and fairly new to Programming. This would be my first real program: #Coordinate Geometry (The whole program is not...
176
by: nw | last post by:
Hi, I previously asked for suggestions on teaching testing in C++. Based on some of the replies I received I decided that best way to proceed would be to teach the students how they might write...
14
by: cs | last post by:
Hi, I'm new to C and would appreciate any feedback on the following program, asplit, which splits a file into 2 new files, putting a certain number of lines in the first file, and all the rest...
7
by: jamaicaboy | last post by:
This is the code that i need some help with........... #include<iostream.h> #include<iomanip.h> class test { public: void store( int);
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.