Number Factorization

Good day everyone.
The task is to factorize a long number > 10 digits. Program works OK until I input a >= 12 digit number. What can be the reason? (Not enough memory?)

Code is placed into two files:

LinkedList.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
using namespace std;

//Prime & getMax( int Input, Prime *First );
//bool isPrime ( long long Number );

class Prime
{
public:
	~Prime() { cout << "Destrucgtor\n"; --NumberOfPrimes; }
	Prime ( long long A ):itsValue (A), itsNext (0) { ++NumberOfPrimes;}

	Prime * getNext() { return itsNext; }
	void insert( Prime * );
	long long multiply() { if ( itsNext ) return (itsValue * ( itsNext -> multiply()) ); return itsValue; }
	long long getValue() { return itsValue; }
	void display();
	static int NumberOfPrimes;
private:
	long long itsValue;
	Prime * itsNext;
};

int Prime::NumberOfPrimes = 0;

void Prime::insert( Prime * newPrime )
{
	if ( !itsNext )
		itsNext = newPrime;
	else
		itsNext -> insert ( newPrime );
}

bool isPrime ( long long Number )
{	
	if ( Number == 2 ) return true;
	else
	{
		if ( (Number % 2) )
			return true;
		else
			return false;
	}
}

/*Prime & getMax( int Input, Prime *First )
{

}*/

void Prime::display ()
{
	if ( itsNext )
	{
		cout << this -> getValue() << endl;
		itsNext -> display();
	}
	else
	cout << this -> getValue() << endl;
}



PrimeFactors.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "LinkedList.h"

void main ( int argc, char *argv[] )
{
	long long Input = 0;
	printf_s ( "%s\n", "Please input the number" );
	//scanf_s ( "%d", & Input );
	cin >> Input;
	long long Test = Input;

	long long Number = 1;
	Prime * First = new Prime ( Number );
	Prime * pPrime = new Prime (0);

	while (1)
	{
		++Number;		//Number = 2
		/*cout <<"Number is\t"<< Number << endl;
		cout <<"Input is\t"<< Input << endl;*/
		if ( isPrime ( Number ) ) 
		{
			while ( Input % Number == 0 )
				{
					pPrime = new Prime ( Number );
					First -> insert ( pPrime );
					Input = Input / Number;
				}
		}
			if ( Test == ( First -> multiply() ) ) break;
	}
	First -> display();
	cout << "Verification" << endl;
	cout << First -> multiply() << endl;
	
	system ("PAUSE");
}


It works perfectly fine for me, I tried 15 digits. I don't know how accurate it is, but it seems to be working correctly.

Edit: If you believe you're running out of memory, it isn't much more, but you could try unsigned long long as long as you don't need negative numbers.

Edit: Or even try a long long double...I believe that's the correct datatype, even a long double is very large.
Last edited on
Sorry but I don't know more thing about this. I am fresher yet.
@Volatile Pulse: there's no such thing as a long long double.
Volatile Pulse (248) I would be grateful If you tried this number 600851475143 and posted the results. Thanks.
http://prntscr.com/ambq3

There is your results.
Topic archived. No new replies allowed.