Here is code for hexagonal and pentagonal numbers. I need type that stores something bigger than 10 billion. Double works fine to about 8.4 billion. Of course I tried using long long and unsigned long long but it's still not working. Please repair this code because it overflows now matter what I do or explain how to use GMP library. I'm using CodeBlocks 10.5. Best regards.

#include <stdio.h>
#include <math.h>

double Pentagonal(int n){
	double P = 0;
	P = (3*n*n - n) / 2;
	P = -P;
	return P;
}
double Equation(){
	double x1,x2,remainder = 0;
	//where the cycle starts
	int n = 166;
	//variable for the remainder
	int x = 0;
	//quadratic equation quotients
	int a = 2;
	int b = -1;
	double c = 0;
	for(n; n<33000; n++)
		{c = Pentagonal(n);
		double expression = b*b - 4*a*c;
		printf("\n%.0lf",expression);
		double root = sqrt(expression);
		x1 = (-b + root)/(2*a);
		x = (-b + root)/(2*a);
		remainder = fmod(x1,x);
		x2 = (-b - root)/(2*a);
		if(remainder == 0)
			{printf("%.4lf",x1);
			printf("\nNumber is %.0lf\n",-c);
		break;}
		}
	return 0;
}
int main(){
	Equation();
	return 0;
}

Recommended Answers

All 2 Replies

You can write your own large number library using character arrays to hold each digit. Here is one such library (c++ class)

Just download, uncompress and compile the GNU Bignum files from here. Use:

#include<gmp.h>

in all your files. Now you can use GMP data types like mpz_t to work with arbitrary precision numbers.

Add, -lgmp flag (i,e link the GMP library) while compiling your final program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.