Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- // 180
- // 3 3 5 2 2
- // 6 sqrt (5)
- int FPartZero(float N) {
- if(ceil(N) != N) return(0);
- return(1);
- }
- int FindOneFactor(unsigned char Byte) {
- int i;
- for(i=2; i<Byte ; i++ )
- if(FPartZero((float)Byte / i))
- return(i);
- return(-1);
- }
- typedef struct TreeNode{
- unsigned char A;
- struct TreeNode *X;
- struct TreeNode *Y;
- } TreeNode;
- TreeNode *NewNode(unsigned char A) {
- TreeNode *Poke = (TreeNode*)malloc(sizeof(TreeNode));
- if(Poke == NULL) abort();
- Poke->A = A;
- Poke->X = NULL;
- Poke->Y = NULL;
- return(Poke);
- }
- void ConstructTree(TreeNode *Now) {
- int Div = FindOneFactor(Now->A);
- if(Div == -1) { // prime
- Now->X = NULL;
- Now->Y = NULL;
- return;
- }
- else {
- Now->X = NewNode(Div);
- Now->Y = NewNode(Now->A/Div);
- // printf(" %i / %i = %i \n", Now->A, Now->X->A, Now->Y->A);
- ConstructTree(Now->X);
- ConstructTree(Now->Y);
- }
- }
- void TraverseTree(TreeNode *Now, char *Count) {
- if(Now->X == NULL || Now->Y == NULL) {
- Count[Now->A]++;
- return;
- }
- TraverseTree(Now->X, Count);
- TraverseTree(Now->Y, Count);
- free(Now);
- }
- short SimplifySqrt(unsigned char Byte) {
- unsigned short Lo, Hi; double F;
- unsigned char CountOfEachFactor[256];
- memset(CountOfEachFactor, 0, 256);
- Lo = Hi = 0;
- printf("%i - ", Byte);
- if(FPartZero(sqrt((float)Byte))) {
- printf("perfect! ");
- Lo = Byte;
- Hi = 0;
- }
- else {
- TreeNode *Seed = (TreeNode*)malloc(sizeof(TreeNode));
- Seed->A = Byte;
- ConstructTree(Seed);
- printf("tree made? - ");
- TraverseTree(Seed, CountOfEachFactor);
- printf("tree counted? -");
- Lo = 1;
- int i, j;
- for(i=0;i<256;i++) {
- j = 0;
- while(CountOfEachFactor[i] >= 2) {
- CountOfEachFactor[i]-=2;
- j++;
- }
- if(j) {
- Lo*=(j*i);
- }
- }
- Hi = 1;
- for(i=0;i<256;i++) {
- j = 0;
- while(CountOfEachFactor[i] > 0) {
- CountOfEachFactor[i]--;
- Hi*=i;
- }
- }
- }
- printf("- %i*sqrt(%i) \n", Lo, Hi);
- }
- int main() {
- printf("%i \n", FindOneFactor(90));
- // SimplifySqrt(48);
- // SimplifySqrt(56);
- // SimplifySqrt(90);
- // SimplifySqrt(81);
- SimplifySqrt(180);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement