Difference between revisions of "Moscow 5"
Jcfernandez2 (talk | contribs) (Created page with '== Assignment 1 == This is a KenKen puzzle solver using a brute force algorithm implementing recursive backtracking to solve a KenKen puzzle. The program creates a possible map …') |
Jcfernandez2 (talk | contribs) (→Assignment 1) |
||
Line 1: | Line 1: | ||
== Assignment 1 == | == Assignment 1 == | ||
− | This is a | + | This is a Pi calculator that calculates pi up to n digits using the arctan formula pi=16arctan(1/5)-4arctan(1/239) as the main formula along with another variation of the formula based on the same method. |
<br/> | <br/> | ||
− | + | ||
<br/> | <br/> | ||
Line 13: | Line 13: | ||
<br/> | <br/> | ||
− | The | + | The arctan method: |
<big><pre> | <big><pre> | ||
− | |||
− | + | void arctan(int multiplier, int denom, int sign) | |
+ | { | ||
+ | INDEXER x; | ||
+ | LONG remain, temp, divisor, denom2; | ||
+ | SHORT NotZero = 1; | ||
+ | INDEXER adv; | ||
− | + | for (x = 0; x < size; x++) | |
+ | powers[x] = 0; | ||
− | + | divisor = 1; | |
+ | denom2 = (LONG)denom;denom2 *= denom2; | ||
+ | adv = 0; | ||
− | + | remain = (LONG)multiplier * denom; | |
+ | while (NotZero) | ||
+ | { | ||
+ | for (x = adv; x < size; x++) | ||
+ | { | ||
+ | temp = (LONG)powers[x] + remain; | ||
+ | powers[x] = (SHORT)(temp / denom2); | ||
+ | remain = (temp - (denom2 * (LONG)powers[x])) * BASE; | ||
+ | } | ||
− | + | remain = 0; | |
+ | for (x = adv; x < size; x++) | ||
+ | { | ||
+ | temp = (LONG)powers[x] + remain; | ||
+ | term[x] = (SHORT)(temp / divisor); | ||
+ | remain = (temp - (divisor * (LONG)term[x])) * BASE; | ||
+ | } | ||
+ | remain = 0; | ||
− | + | if (sign > 0) | |
+ | { | ||
+ | LONG carry, sum; | ||
− | + | carry = 0; | |
+ | for (x = size - 1; x >=0; x--) | ||
+ | { | ||
+ | sum = (LONG)pi[x] + (LONG)term[x] + carry; | ||
+ | carry = 0; | ||
+ | if (sum >= BASE) | ||
+ | { | ||
+ | carry = 1; | ||
+ | sum -= BASE; | ||
+ | } | ||
+ | pi[x] = (SHORT)sum; | ||
+ | } | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | LONG borrow, sum; | ||
− | + | borrow = 0; | |
+ | for (x = size - 1; x >= 0; x--) | ||
+ | { | ||
+ | sum = (LONG)pi[x] - (LONG)term[x] - borrow; | ||
+ | borrow = 0; | ||
+ | if (sum < 0) | ||
+ | { | ||
+ | borrow = 1; | ||
+ | sum += BASE; | ||
+ | } | ||
+ | pi[x] = (SHORT)sum; | ||
+ | } | ||
+ | } | ||
− | + | sign = -sign; | |
+ | divisor += 2; | ||
+ | NotZero = 0; | ||
+ | for (x = adv; x < size; x++) | ||
+ | { | ||
+ | if (powers[x]) | ||
+ | { | ||
+ | NotZero = 1; | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | if (NotZero) | ||
+ | { | ||
+ | while (powers[adv] == 0) | ||
+ | adv++; | ||
+ | } | ||
+ | /* We can skip ones that are already 0 */ | ||
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } |
Latest revision as of 09:49, 6 December 2012
Assignment 1
This is a Pi calculator that calculates pi up to n digits using the arctan formula pi=16arctan(1/5)-4arctan(1/239) as the main formula along with another variation of the formula based on the same method.
We will be optimizing the following functions:
The arctan method:
void arctan(int multiplier, int denom, int sign) { INDEXER x; LONG remain, temp, divisor, denom2; SHORT NotZero = 1; INDEXER adv; for (x = 0; x < size; x++) powers[x] = 0; divisor = 1; denom2 = (LONG)denom;denom2 *= denom2; adv = 0; remain = (LONG)multiplier * denom; while (NotZero) { for (x = adv; x < size; x++) { temp = (LONG)powers[x] + remain; powers[x] = (SHORT)(temp / denom2); remain = (temp - (denom2 * (LONG)powers[x])) * BASE; } remain = 0; for (x = adv; x < size; x++) { temp = (LONG)powers[x] + remain; term[x] = (SHORT)(temp / divisor); remain = (temp - (divisor * (LONG)term[x])) * BASE; } remain = 0; if (sign > 0) { LONG carry, sum; carry = 0; for (x = size - 1; x >=0; x--) { sum = (LONG)pi[x] + (LONG)term[x] + carry; carry = 0; if (sum >= BASE) { carry = 1; sum -= BASE; } pi[x] = (SHORT)sum; } } else { LONG borrow, sum; borrow = 0; for (x = size - 1; x >= 0; x--) { sum = (LONG)pi[x] - (LONG)term[x] - borrow; borrow = 0; if (sum < 0) { borrow = 1; sum += BASE; } pi[x] = (SHORT)sum; } } sign = -sign; divisor += 2; NotZero = 0; for (x = adv; x < size; x++) { if (powers[x]) { NotZero = 1; break; } } if (NotZero) { while (powers[adv] == 0) adv++; } /* We can skip ones that are already 0 */ } }