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; }
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; }
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; }
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; }
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); }
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 }
static void NpcSpellAttack(P_CHAR pc_attacker, P_CHAR pc_defender, unsigned int currenttime, int los) { if (pc_attacker->spatimer<=currenttime) { int spattacks = numbitsset( pc_attacker->spattack ); if (!pc_defender->dead && chardist(pc_attacker, pc_defender) < SrvParams->attack_distance() && spattacks > 0 ) { if (los) { int spattackbit=rand()%(spattacks) + 1; switch(whichbit(pc_attacker->spattack, spattackbit)) { case 1: if (pc_attacker->mn>=4) { npcaction(pc_attacker, 6); Magic->MagicArrow(pc_attacker, pc_defender); } break; case 2: if (pc_attacker->mn>=6) { npcaction(pc_attacker, 6); Magic->HarmSpell(pc_attacker, pc_defender); } break; //lb case 3: if (pc_attacker->mn>=4) { npcaction(pc_attacker, 6); Magic->ClumsySpell(pc_attacker, pc_defender); } break; //LB case 4: if (pc_attacker->mn>=4) { npcaction(pc_attacker, 6); Magic->FeebleMindSpell(pc_attacker, pc_defender); } break; //LB case 5: if (pc_attacker->mn>=4) { npcaction(pc_attacker, 6); Magic->WeakenSpell(pc_attacker, pc_defender); } break; //LB case 6: if (pc_attacker->mn>=9) { npcaction(pc_attacker, 6); Magic->FireballSpell(pc_attacker, pc_defender); } break; //LB case 7: if (pc_attacker->mn>=11) { npcaction(pc_attacker, 6); Magic->CurseSpell(pc_attacker, pc_defender); } break; //LB case 8: if (pc_attacker->mn>=11) { npcaction(pc_attacker, 6); Magic->LightningSpell(pc_attacker, pc_defender); } break; //lb case 9: if (pc_attacker->mn>=14) { npcaction(pc_attacker, 6); Magic->ParalyzeSpell(pc_attacker, pc_defender); } break; //lb case 10: if (pc_attacker->mn>=14) { npcaction(pc_attacker, 6); Magic->MindBlastSpell(pc_attacker, pc_defender); } break; case 11: if (pc_attacker->mn>=20) { npcaction(pc_attacker, 6); Magic->EnergyBoltSpell(pc_attacker, pc_defender); } break; case 12: if (pc_attacker->mn>=20) { npcaction(pc_attacker, 6); Magic->ExplosionSpell(pc_attacker, pc_defender); } break; case 13: if (pc_attacker->mn>=40) { npcaction(pc_attacker, 6); Magic->FlameStrikeSpell(pc_attacker, pc_defender); } break; case 14: npcaction(pc_attacker, 6); Magic->PFireballTarget(pc_attacker, pc_defender, 10); break; case 15: npcaction(pc_attacker, 6); Magic->PFireballTarget(pc_attacker, pc_defender, 20); break; case 16: npcaction(pc_attacker, 6); Magic->PFireballTarget(pc_attacker, pc_defender, 40); break; } } } pc_attacker->spatimer=currenttime+(pc_attacker->spadelay*MY_CLOCKS_PER_SEC); //LB bugkilling } }