Program won't run with really big numbers - c++

I'm pretty new to programming and so I"m making this program in C++ that will take a number and find it's prime factors, which works great! Unless it's too big for an int variable. Now then I tried to change all of the int variables to long long variables so it wouldn't matter, but this doesn't seem to fix the problem. The program is as follows:
#include <iostream>
using namespace std;
bool prime (long long recievedvalue) { //starts a function that returns a boolean with parameters being a factor from a number
long long j =1;
long long remainderprime = 0;
bool ended = false;
while (ended == false){ //runs loop while primality is undetermined
if (recievedvalue == 1){ //if the recieved value is a 1 it isn't prime
//not prime
return false;
break; // breaks loop
}
remainderprime=recievedvalue%j; //gives a remainder for testing
if ((remainderprime==0 && j>2) && (j!=recievedvalue || j == 4)){ //shows under which conditions it isn't prime
ended=true;
//not prime
return false;
}
else if (j==1){
j++;
}
else if ( recievedvalue==2 || j==recievedvalue ){ // shows what conditions it is prime
ended = true;
//prime
return true;
}
else {
j++;
}
}
}
long long multiple(long long tbfactor){ //factors and then checks to see if factors are prime, then adds all prime factors together
//parameter is number to be factored
long long sum = 0;
bool primetest = false;
long long remainderfact;
long long i=1;
while (i<=tbfactor){ //checks if a i is a factor of tbfactor
remainderfact=tbfactor%i;
if (remainderfact==0){ //if it is a factor it checks if it is a prime
primetest = prime(i);
}
if (primetest ==true){ //if it is prime it add that to the sum
sum += i;
primetest=false;
}
i++;
}
return sum;
}
int main()
{
long long input;
long long output;
cout << "Enter a number > 0 to find the sum of all it's prime factors: ";
cin >> input;
if (input == 0 || input <= 0){
cout << "The number you entered was too small."<< endl << "Enter number a number to find the sum of all it's prime factors: ";
cin >> input;
}
output = multiple(input);
cout << output << endl << "finished";
return 0;
}
Now then to be sure, the problem does the same thing whether or not it's a int or not. Also like I said I"m new to programming, and C for that matter so I look forward to your easily understandable replies. :)

I'm willing to be that your program IS running. I'm sure that someone is going to pop on and give you the answer in a heartbeat, but I'm hoping that it doesn't happen so that you get to experience the same thing that I did when I ran into the problem YEARS ago.
Do this: start with 1, and work up from there using powers of 2 (1, 2, 4, 8, 16, etc.) and just keep going, doubling the input number each time. When does it "stop running?" Does it get progressively slower?
Please comment back on my post or on your own, or edit your own, or post an answer, whatever it is you're allowed to do with only 56 rep. If the community will allow it (and of course I would like the community to further the lesson), I'd like to gently push you to the answer through a series of back-and-forth steps feedback rather than the typical fashion, since this is an obvious unique learning opportunity.

If you are trying to find if a number is a prime or not, here is a fast solution,
#include <iostream>
using namespace std;
#define ullong unsigned long long
bool prime (ullong x)
{
if(x <= 1)
return false;
ullong s = (ullong)sqrt(x);
for(ullong i=2;i<=s;i++)
if(x%i == 0)
return false;
return true;
}

Related

Trying out a problem but code I had written is not giving output for large numbers. why?

I was trying this question.
The prime factors of 13195 are 5, 7, 13 and 29.What is the largest prime factor of the number 600851475143 ?
And I had written the following code:
#include<iostream>
#define num 600851475143
using namespace std;
int isprime(unsigned long long int n)
{
unsigned long long int c=0;
for(unsigned long long int i=2;i<n;i++)
{
if(n%i==0)
{
c++;
break;
}
}
if(c==0)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
unsigned long long int a,i,n=num;
while(n-- && n>1)
{
if(isprime(n)==1 && num%n==0)
{
cout<<n;
break;
}
}
return 0;
}
The problem occurring with the code is it is working for 13195 and other small values. But not getting any output for 600851475143. Can anyone explain why it is not working for large value and also tell the changes that should be made in these to get the correct output.
The below code snippets are from c (but should run quite nice with c++ as well):
#include <stdio.h>
#define uIntPrime unsigned long long int
#define uIntPrimeFormat "llu"
uIntPrime findSmallestPrimeFactor(uIntPrime num)
{
uIntPrime limit = num / 2 + 1;
for(uIntPrime i=2; i<limit; i++)
{
if((num % i) == 0)
{
return i;
}
}
return num;
}
uIntPrime findLargestPrimeFactor(uIntPrime num)
{
uIntPrime largestPrimeFactor = 1; // start with the smallest possible value
while (num > 1) {
uIntPrime primeFactor = findSmallestPrimeFactor(num);
if (primeFactor > largestPrimeFactor) largestPrimeFactor = primeFactor;
num = num / primeFactor;
}
return largestPrimeFactor;
}
How can this work?
(first function:) Counting the numbers up from 2 means you are starting with prime factors on the lower end. (Numbers that are non-prime when counting are just not working out as fraction-less divisors and at the same time their prime number factor components were already probed because they are lower.)
(second function:) If a valid factor is found then the factor is pulled out from the number in question. Thus the search for the now smallest prime in the pulled-out number can repeat. (The conditional might probably be superfluous due to lower numbers are found first anyway - but it might resemble a search pattern you are familiar with - like in a minimum/maximum/other-criteria search. I am now leaving it up to you to proof it right or wrong with testing with your own main routine.)
The stop condition is about having the last factor extracted means dividing the value by itself and getting a value of 1 for num.
(There is for sure still much space for speeding this up!)

Prime numbers between two given integers

This is my code for finding prime numbers between two integers. It compiles alright but giving a runtime error SIGXFSZ on codechef.
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n,m;
int t;
cin>>t;
while(t--)
{
cin>>m>>n;
for(long long j=m;j<=n;j++)
for(long long i=2;i<=sqrt(j);i++)
if(j%i==0)
break;
else cout<<j<<"\n";
cout<<"\n";
}
return 0;
}
Seems that you are wrong on logic.
According to my understanding, you are supposed to print the prime numbers between two numbers.
But your code has logical errors.
1) Code doesn't consider 2 and 3 as prime numbers.
Say, m = 1, n = 10. For j = 2, 3, the inner loop won't execute even for the single time. Hence, the output won't be shown to be user.
2) else cout<<j<<"\n"; statement is placed incorrectly as it will lead to prime numbers getting printed multiple times and some composite numbers also.
Example:
For j = 11, this code will print 11 twice (for i = 2, 3).
For j = 15, this code will print 15 once (for i = 2) though it is a composite number.
You've underexplained your problem and underwritten your code. Your program takes two separate inputs: first, the number of trials to perform; second, two numbers indicating the start and stop of an individual trial.
Your code logic is incorrect and incomplete. If you were to use braces consistently, this might be clear. The innermost loop needs to fail on non- prime but only it's failure to break signals a prime, so there can't be one unless the loop completes. The location where you declare a prime is incorrect. To properly deal with this situation requires some sort of flag variable or other fix to emulate labelled loops:
int main() {
int trials;
cin >> trials;
while (trials--)
{
long long start, stop;
cin >> start >> stop;
for (long long number = start; number <= stop; number++)
{
if (number < 2 || (number % 2 == 0 && number != 2))
{
continue;
}
bool prime = true;
for (long long odd = 3; odd * odd <= number; odd += 2)
{
if (number % odd == 0)
{
prime = false;
break;
}
}
if (prime)
{
cout << number << "\n";
}
}
}
return 0;
}
The code takes the approach that it's simplest to deal with even numbers and two as a special case and focus on looping over the odd numbers.
This is basically "exceeded file size", which means that the output file is having size larger than the allowed size.
Please do check the output file size of your program.

Failing prime factorisning program (c++)

Im trying to solve this problem on programing.
Here is the question.
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
Now I've cooked up a c++ program, which tries to check it by brute force, however, while executing its stuck at 5. Here is the Program
#include <iostream>
#include <math.h>
using namespace std;
const long long no = 600851475143;
long long isprime(long long p)
{
long long reply = -1;
long long i = 2;
while (i < pow(p, 0.5)) {
if (i % p == 0)
reply = i;
}
if (reply == -1){
return 0;
cout<<" yup its prime "<<endl;
}
else
return reply;
}
long long factor(long long x)
{
for (long long i = 2; i < no; i++) {
cout<<"Trying "<<i<<endl;
if ((isprime(i) == 0)&& (no % i == 0)) {
return i;
cout<<"found "<<i<<endl;
break;
}
}
}
int main()
{
long long ans = no;
while (ans != 1) {
cout << factor(ans) << endl;
ans = ans / factor(ans);
}
}
and this is the output
~/Desktop/proj$ ./a.out
Trying 2
Trying 3
Trying 4
Trying 5
I really don't understand why its stuck at number 5, can someone help me out?
EDIT : Thanks b13rg , I realised my mistake . I now have a better algorithm , I have pasted it down for anybody needing it.
#include<iostream>
#include<math.h>
using namespace std;
long long fun (long long x)
{
for(long long i=2; i<sqrt(x);i++){
while (x%i==0){
cout<<i<<endl;
x=x/i;
}
}
}
int main(){
fun(600851475143);
return 0;}
You seem to never change the value of i or p in the while loop in the function isprime. It fails because sqrt(5) is larger than 2, and nothing in the while loop ever changes.
For the problem you're trying to solve:
You could first optimize the checking loop by only trying odd numbers, so first do 2, then 3, then 5 etc. To make it even faster you could hard code in the first few primes, but that might be beyond the scope of this project.
To find the largest prime factor, you would want to first find the smallest prime factor. For example, in the number above, the smallest is 5. The next step would be to divide 13195 by 5 to get 2369. Then start again to find the smallest prime factor of this number, and keep going until the dividing result is prime.
Number|Smallest prime
------|--------------
13169 | 5
2369 | 7
377 |13
29 |Largest prime factor of 13169

Project Euler challenge 3: Finding the largest prime factor of a large number [duplicate]

This question already has answers here:
Finding largest prime number out of 600851475143?
(3 answers)
Closed 9 years ago.
Can't find the prime factor of 600851475143 for projecteuler. My code successfully computes the largest prime factor of the test number 13195 and every test number I throw at it, but somehow it degrades with the large prime number. Do you know why?
#include <iostream>
#include <queue>
using namespace std;
int split(int split);
int largestprimefactor(priority_queue<int> myints);
int main()
{
int response = 2;
do{
priority_queue<int> myints;
int number;
cout << "Please enter a number: ";
cin >> number;
myints.push(number);
int lcf = largestprimefactor(myints);
cout << endl << "Largest prime factor is: " << lcf;
cout << endl << "Again?(1 for yes 2 for no): ";
cin >> response;
}while(response == 1);
}
uint64_t split(uint64_t split)
{
if(split%2 != 0)
{
if((split/2))%2 == 0)
for(uint64_t i = (split/2)-1; i>1; i=i-2)
if(split%i == 0)
return i;
else
for(uint64_t i = (split/2); i>1; i=i-2)
if(split%i == 0)
return i;
return 1;
}
else
return 2;
}
int largestprimefactor(priority_queue<int> myints)
{
// largestfactor holds the next number to be tested for primeness in the queue
do{
int largestfactor = myints.top();
myints.pop();
//splat will hold the first factor split finds of the top item in the queue
int splat = split(largestfactor);
//if it holds a 1 then that means that there are no factors
if(splat != 1 && largestfactor)
{
myints.push(splat);
myints.push(largestfactor / splat);
}
else
return largestfactor;
}while(myints.top() > 1);
}
Have you considered that 600851475143 is too large to store in a 32 bit int?
Look into what your compiler provides for 64 bit integer types.
I might not be able to help you optimize your code (I'm not sure what you do in split), but here's an idea.
By the fundamental theorem of arithmetic, each number has a unique factorization into a product of primes. This means we can take a number and successively divide it by its prime factors until we reach 1. The last prime factor is the answer.
Now, you need only check prime factors up to sqrt(N). Note that this does not mean that the largest prime factor is less than sqrt(N), but that if there is a prime factor greater than sqrt(N), there is only one such prime factor.
This leads to the following O(sqrt(N)) algorithm:
long long largest_factor(long long number) {
long long result = 0;
for (long long i = 2; i * i <= number; ++i) {
if (number % i == 0) {
result = i;
while (number % i == 0)
number /= i;
}
}
if (number != 1)
return number;
return result;
}
Running this on 600851475143 gives me the right answer.
600851475143 >> 32 give 129, 600851475143 >> 64 give 3.10^-8. This number is too big to be represented as an int, but you can represent it with a 64 bit number as long long or a class designed to represent bigger integers.

Project Euler 3 - Highest Prime Factor

before I start I want to clarify that I am not looking for code examples to get the answer; that would defeat the object of Project Euler.
The problem can be found here http://projecteuler.net/problem=3
I think I have a way of solving the problem, but the Algorithm is VERY slow; it has been running for nearly two and a half hours now. So I am looking for general advice on optimisation.
Thanks.
#include<iostream>
using namespace std;
bool primality(int);
int main(){
long long lim = 600851475143;
long long div = lim/2;
bool run = true;
while(run){
if(lim%div==0 && primality(div)){
cout << "HPF: " << div;
run = false;
}
else{
div--;
}
if(div<=1){
break;
}
}
return 0;
}
bool primality(int num){
for(int i=2; i<num; i++){
if(num%i==0 && i!=num){
return false;
}
else{
return true;
}
}
}
If you start div at 2 and count up instead of down, and divide it out from the number when the modulo is zero, you gain two big advantages that are useful here:
You don't have to check if div is prime, since it can't be composite because any prime factors smaller than it would already have been divided out.
You reduce the remaining problem size every time you find a factor, and, as it turns out, the input number has fairly small prime factors.
You could then also break once div*div is greater than the remaining number, as you know at that point that it must be a prime. This is because any divisors greater than the square root are "paired" with one less than the square root. However, since this is an "easy" problem, this optimization is not needed here (although it is useful for later problems).
# Possible solution but still its *time consuming* but answer can be guessed by the last option in console output
#include<stdio.h>
#include<string>
#include<iostream>
#include<math.h>
int prime(unsigned long long);
using namespace std;
int main(){
unsigned long long ii, ij; unsigned long long in;
cin>>in; ij = ceil(in/2);
if( (ij % 2) == 0 ) ij -= 1;
for(ii = 3 ;ii < ij;ii+= 2){
if(in % ii == 0){
if(prime(ii) == 1 ){
cout<<" ans "<<ii<<endl;
}
}
}
return 0;
}
int prime(unsigned long long ii){
unsigned long long ij;
for(ij = 3;ij < ii/2 ;ij += 2){
if( (ii % ij) ==0){
return 0;
}
}
return 1;
}