[ create a new paste ] login | about

Link: http://codepad.org/afrvIcjs    [ raw code | output | fork ]

C++, pasted on Jan 5:
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
class LongHex{
public:
	int mod(unsigned long);
};
int LongHex::mod(unsigned long value){
	union LL {
		unsigned long long ull;
		unsigned long ul[2];
	}temp;
	int pos = value / 64;
	unsigned long mod = value % 64;
	int i, lc = pos + 1;
	//int neg = 0xffffffff;
	unsigned long long num = pow(2, mod);
	unsigned long div, reserve; 
	num--; mod = 0;			//num = 2^n - 1
	if(pos == 0)
		num--;			//num = 2^n - 2
	for(i = 0; i < lc; i++){
		temp.ull = num;
		if(i == lc - 1 && pos > 0){
			temp.ull = 0xfffffffe;	//num = 2^n - 2
		}
		reserve = temp.ul[0];
		temp.ul[0] = temp.ul[1];
		temp.ul[1] = mod;
		//div = temp.ull / value;
		mod = temp.ull % value;
		temp.ul[0] = reserve;
		temp.ul[1] = mod;
		mod = temp.ull % value;
		num = 0xffffffff;
	}
	return mod;
}
class PrimeStore{
protected:
	int n;
	int maxvalue;
	int prime[32768];
	void newPrime(int);
public:
	PrimeStore();
	bool isPrime(int);
};
	PrimeStore::PrimeStore(){
		n = 2;
		prime[0] = 2; prime[1] = 3;
		maxvalue = 3;
	}
void	PrimeStore::newPrime(int value){
	prime[n] = value;
	n++;
	maxvalue = value;
}
bool	PrimeStore::isPrime(int value){
	bool set;
	int temp, num;
	if(value == 1)
		return false;
	for(int i = 0; i < n; i++){
		temp = prime[i];
		if(value <= temp){
			return true;
			//cout << " (" << num << ")is primeA";
		}else if((value % temp) == 0){
			return false;
			//cout << " (" << num << ")is not primeA";
		}
	}
	num = maxvalue;
	do{
		num += 2;
		set = true;
		for(int i = 0; i < n; i++){
			temp = prime[i];
			if((num % temp) == 0){
				set = false;
				break;
			}
		}
		if(set == true){
			newPrime(num);
			//cout << " (" << num << ")is new prime";
		}
		if(maxvalue == value || value == num)
			return set;
	} while(num < value);
	return false;
}
int main(int argc, char** argv) {
	LongHex lh;
	PrimeStore ps;
	for(int i = 3; i < 65535; i += 2){
		if(lh.mod(i) == 0){
			//cout << " (" << i << ")mod is zero";
			if(ps.isPrime(i) == false){
				cout << " " << i;
			}else{ 
				//cout << " (" << i << ")is prime";
			}
		}else{
			//cout << " (" << i << ")mod!=0";
		}
	}
	exit(0);
}


Output:
1
2
3
t.cpp: In member function 'int LongHex::mod(long unsigned int)':
Line 11: error: ISO C++ does not support 'long long'
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments: