int main(int argc,char *argv[]) { int Pa(int low,int high,int length,char *str); int iseven(int n); int isodd(int n); char str[]="aabaa"; int len; len = strlen(str); if(Pa(0,len-1,len,str)) { printf("this string is a Pa string\n"); } printf("3:%d 4:%d\n",iseven(0),iseven(4)); }
int PE2main(pe_data_t *pedata) { unsigned int next = 0; unsigned int current = 1, last = 0, totaleven = 0; printf("Running Project Euler Problem 2\n"); while (1) { next = current + last; last = current; current = next; if (next > 4000000) break; if(iseven(next)) totaleven = totaleven + next; /*printf ("Currently at: %i\n", current);*/ } if (pedata->verbosity > 0) printf("Total: %i\n", totaleven); sprintf(pedata->result,"%i",totaleven); return 0; }
int func(int b,int n) { if (n == 0) { return 1; } else { return iseven( n ) ? func ( square(b) , halve( n ) ) : b * func( b , n-1 ) ; } }
char *num2bin(unsigned long long num, char *buf, size_t n) { char s; buf[n-1] = 0; while (--n) { s = iseven(num) ? '0' : '1'; buf[n-1] = s; if (!(num /= 2)) break; } return &buf[n-1]; }
void half_even(int a[], int num){ int left = 0; int right = num - 1; int tmp; if((a == NULL)||(num == 0)){ printf("empty array\n"); exit(1); } while(left < right){ if(iseven(a[left])&&(!iseven(a[right]))){ tmp = a[left]; a[left] = a[right]; a[right] = tmp; } if(!iseven(a[left])) left++; if(iseven(a[right])) right--; } }
int isodd(int n) { return !(iseven(n)); }
// extended binary gcd // // For a anv b it solves, v = gcd(a,b) and finds x and y s.t. // ax + by = v // // Handbook of Applied Cryptography, Menezes et al, 1997, pg 608. void mpextendedgcd(mpint *a, mpint *b, mpint *v, mpint *x, mpint *y) { mpint *u, *A, *B, *C, *D; int g; if(a->top == 0){ mpassign(b, v); mpassign(mpone, y); mpassign(mpzero, x); return; } if(b->top == 0){ mpassign(a, v); mpassign(mpone, x); mpassign(mpzero, y); return; } g = 0; a = mpcopy(a); b = mpcopy(b); while(iseven(a) && iseven(b)){ mpright(a, 1, a); mpright(b, 1, b); g++; } u = mpcopy(a); mpassign(b, v); A = mpcopy(mpone); B = mpcopy(mpzero); C = mpcopy(mpzero); D = mpcopy(mpone); for(;;) { // print("%B %B %B %B %B %B\n", u, v, A, B, C, D); while(iseven(u)){ mpright(u, 1, u); if(!iseven(A) || !iseven(B)) { mpadd(A, b, A); mpsub(B, a, B); } mpright(A, 1, A); mpright(B, 1, B); } // print("%B %B %B %B %B %B\n", u, v, A, B, C, D); while(iseven(v)){ mpright(v, 1, v); if(!iseven(C) || !iseven(D)) { mpadd(C, b, C); mpsub(D, a, D); } mpright(C, 1, C); mpright(D, 1, D); } // print("%B %B %B %B %B %B\n", u, v, A, B, C, D); if(mpcmp(u, v) >= 0){ mpsub(u, v, u); mpsub(A, C, A); mpsub(B, D, B); } else { mpsub(v, u, v); mpsub(C, A, C); mpsub(D, B, D); } if(u->top == 0) break; } mpassign(C, x); mpassign(D, y); mpleft(v, g, v); mpfree(A); mpfree(B); mpfree(C); mpfree(D); mpfree(u); mpfree(a); mpfree(b); }