Example #1
0
void setBit(int j) {
   seg* segNum =  whichseg(j);
   int rownum = whichint(j);
   int posinrow = whichbit(j);
   
   seg *p = segNum;
   unsigned int flag = 1;
   flag = flag << posinrow;
   p->bits[rownum] = p->bits[rownum] | flag;
}
Example #2
0
int testBitIs0( int j ) {

   seg* segNum =  whichseg(j);
   int rownum = whichint(j);
   int posinrow = whichbit(j);

 
   int a = 0;
   seg *p = segNum;
   unsigned int flag = 1;
   flag = flag << posinrow;
   int r = p->bits[rownum] & flag; 

   if ( r == 0 )
      return 1;        
   else
      return 0;
}
Example #3
0
int   testbit (int n, seg* head){ 

	 int q = (n-3)/2;

	 seg* thisseg = whichseg(q, head);

	 int thisint = whichint(q);

	 int thisbit = whichbit(q);

	if(thisseg->bits[thisint] & 1 << thisbit)

		return 1;

	else

		return 0;

		}
Example #4
0
void  setbit (int n, seg* head){

	int q = (n-3)/2;

	seg* thisseg = whichseg(q, head);

	int thisint = whichint(q);

	int thisbit = whichbit(q);

	

	thisseg->bits[thisint] |= 1 << thisbit;

	

	

	} 
Example #5
0
void goldbach(int n){
// finds the seg, int and bit for 3 and k-3 and passes them to methods that increment up and down in 2 different methods
int numCount = 0;
int k = 3;
int ktwo = n-3;
int goldCount = 0;
int targetNum1 = 0;
int targetNum2 = 0;
///////seg int and bit for 3
seg* segNum2 = whichseg(k);
int kintone = whichint(k);
int bit1 = whichbit(k);
///////seg in and bit for k-3
seg* segNum3 = whichseg(ktwo);
int kinttwo = whichint(ktwo);
int bit2 = whichbit(ktwo);

while (k <= n/2){
		
	if(checkLower(segNum2, kintone, bit1) && checkHigher(segNum3, kinttwo, bit2) == 1){
		goldCount += 1;
		targetNum1 = k;
		targetNum2 = ktwo;
		}

	//printf("%d %d\n", k, ktwo);

	//////////////////////////////////LOWER END LOGIC
	if(bit1 != 31){
		bit1 +=1;}
		
	else{
	bit1 = 0;
	kintone += 1;}

	if(kintone > 255 && segNum2->next !=NULL){
	kintone = 0;
	segNum2 = segNum2->next;}
	/////////////////////////////////////////////////
	//////////////////////////////////UPPER END LOGIC
	if(bit2 != 0){
		bit2 -=1;}

	else{
	bit2 = 31;
	kinttwo -= 1;}	
	
   	if( kinttwo < 0 && segNum3->prev != NULL){
	kinttwo = 255;
	segNum3 = segNum3->prev;
	}	
	/////////////////////////////////////////////////


	k += 2;
	ktwo -=2;

}
printf("\nThe last Goldbach decomposition is: %d + %d \n",  targetNum1, targetNum2 );
printf("The amount of decompositions is: %d \n", goldCount);	
	

}
Example #6
0
void main(int argc, char *argv[]) {

	seg *head,*pt;
	int	 i;
	int howmany, upto;

	//goldbach sample from class only worked if arg was sent in with the ./goldbach
	if (argc == 2) sscanf(argv[1],"%d",&howmany);
		else return;
	upto = howmany;
	howmany = ((howmany/2) +BITSPERSEG -1)/BITSPERSEG;

	head = (  seg * ) malloc(sizeof(seg));
	pt=head;
	
	//link the segments created
	for (i=1;i<howmany;i++) {
		pt->next = (  seg *) malloc(sizeof (seg)); 
		pt->next->prev = pt;
		pt=pt->next;	
	}
	//printf("Done allocating %d nodes\n",i);
	int j;
	pt=head;
	//clear int's at a time for each segment
	for (i=1;i<=howmany;i++) {
		for(j=0;j<255;j++) {
		pt->bits[j] = 0;
		}
	pt=pt->next;
	//printf("Done clearing node %d \n",i);
	}

	//find the primes
printf("Calculating odd primes up to %d...\n",upto);
	//sqrt() wasnt working so used i^2
	
	for(i = 3 ; (i*i)<=upto ; i+=2) {
		seg* myPT = whichseg(i,head);
		int myInt = whichint(i);
		int myBit = whichbit(i);
	//after or'ing with the bit pos set to one, if its unchanged that means the number was turned on 
		if((myPT->bits[myInt]|(1<<myBit)) == myPT->bits[myInt]) {
			//the number isnt prime
			continue;
		} else {
			//we have a prime so remove its multiples!!!
			int j;
			for( j = i+i; j<=upto; j+=i) {
				if((j%2)!=0) {
					seg* nonprimePT = whichseg(j,head);
					int nonprimeInt = whichint(j);
					int nonprimeBit = whichbit(j);	
					nonprimePT->bits[nonprimeInt] = nonprimePT->bits[nonprimeInt]|(1<<nonprimeBit);	
				}
			}
		}
	}

	//count the primes
	int countPrimes =0;
	pt=head;
	int finalInt = whichint(upto);
	int finalBit = whichbit(upto);
	int segment =1;
	int num = 3;
	for(segment=1; segment<=howmany; segment++) {
		for(i = 0 ; i<=255 ; i++) {
			for(j = 0 ; j<32;j++) {
				if (((pt->bits[i]|(1<<j))) == pt->bits[i]) {
					//printf(" number not prime: %d: data:i- %d and j- %d \n", num,i,j);
					num+=2;
					continue;
				} else {
					if(num>upto) {
					//done counting
					break;
					} else {
					//found a prime!
					countPrimes++;
					}
				}
				num+=2;
			}
		}
	pt= pt->next;
	}
	printf("The number of odd primes less than or equal to %d is:  %d \n",upto,countPrimes);
	
//now from prime numbers calc the possible pair sums
	printf("Enter Even Numbers >5 for Goldbach Tests\n");
	int even;
	scanf("%d",&even);
	int lasteven = 0;
	while(even) {
		int sumCounter = 0;
		int lasti = 0;
		//when a non-int was entered, my program would freakout.
		if(even == lasteven) {break;}
		//end on EOF
		if(even == EOF) { break;}
		//must be even number and greater than 5 and less than that to which i calc primes for
		if(even >= 6 & even <= upto & even%2==0)	{
			int lowerNum = 3;
			seg* lowerPT = whichseg(lowerNum,head);
			int lowerInt = whichint(lowerNum);
			int lowerBit = whichbit(lowerNum);
			
			int higherNum = even-3;
			seg* higherPT = whichseg(higherNum,head);
			int higherInt = whichint(higherNum);
			int higherBit = whichbit(higherNum);
			//while statement
			while(lowerNum <= higherNum) {
			if((lowerPT->bits[lowerInt]|(1<<lowerBit)) == lowerPT->bits[lowerInt]) {
					//lower is not prime
						//rise lowerbit/ lower higherbit
						if(lowerBit==31) {
							lowerBit=0;
							lowerInt++;
							if(lowerInt==256) {
								lowerInt =0;
								lowerPT=lowerPT->next;
							}
							lowerNum+=2;
						} else {
							lowerBit++;
							lowerNum+=2;
						}
						if(higherBit==0) {
							higherBit=31;
							higherInt--;
							if(higherInt==-1) {
								higherInt =255;
								higherPT=higherPT->prev;
							}
							higherNum-=2;
						} else {
							higherNum-=2;
							higherBit--;
						}
						/////// end of bit manipulation
					continue;
				} else {
					if((higherPT->bits[higherInt]|(1<<higherBit)) == higherPT->bits[higherInt]) {
					//higher is not prime
						//rise lowerbit/ lower higherbit
						if(lowerBit==31) {
							lowerBit=0;
							lowerInt++;
							if(lowerInt==256) {
								lowerInt =0;
								lowerPT=lowerPT->next;
							}
							lowerNum+=2;
						} else {
							lowerBit++;
							lowerNum+=2;
						}
						if(higherBit==0) {
							higherBit=31;
							higherInt--;
							if(higherInt==-1) {
								higherInt =255;
								higherPT=higherPT->prev;
							}
							higherNum-=2;
						} else {
							higherNum-=2;
							higherBit--;
						}
						/////// end of bit manipulation
					continue;
					} else {
						sumCounter++;
						lasti = lowerNum;
						//printf("solution %d: lowerNum- %d, higherNum %d \n",sumCounter,lowerNum, higherNum);
						//rise lowerbit /lower higherbit
						if(lowerBit==31) {
							lowerBit=0;
							lowerInt++;
							if(lowerInt==256) {
								lowerInt =0;
								lowerPT=lowerPT->next;
							}
							lowerNum+=2;
						} else {
							lowerBit++;
							lowerNum+=2;
						}
						if(higherBit==0) {
							higherBit=31;
							higherInt--;
							if(higherInt==-1) {
								higherInt =255;
								higherPT=higherPT->prev;
							}
							higherNum-=2;
						} else {
							higherNum-=2;
							higherBit--;
						}
						/////// end of bit manipulation
					}
				}
			
			} //end while
		//calc the middle term using the value
		printf("Largest %d = %d + %d out of %d solutions \n", even ,lasti, even-lasti,sumCounter);
		}
		lasteven = even;
		scanf("%d",&even);
	} // outer while
}