Monday, September 23, 2019

A Number Properties Detailer in C

So I just threw together something real quick whereby you enter an integer, and it outputs the following properties:

  • Prime or Composite: does the number have factors other than 1 and itself?
  • Even or Odd: is the number divisible by 2 without a remainder?
  • Fibonacci Number: is the number an element of the set of all whereby Fn+1=Fn + Fn-1 where F0 = 1 and F1 = 1?
  • Lucas Number: is the number an element of the set of all whereby Fn+1=Fn + Fn-1 where F0 = 1 and F1 = 3?
  • Perfect Number: does the number's factors add up to itself?
  • Almost Perfect Number: is the number one off from a perfect number? (All known almost perfect numbers are powers of 2).
  • Power of 2: is the number a power of 2?
  • Perfect Square: is the square root of the number an integer?
  • Perfect Cube: is the cube root of the number an integer?
  • Triangular Number: is the number an element of the set of all xn whereby xn = n(n+1)/2 ?
  • Amicable Numbers: for number A and number B, does number A's factors add to number B, and number B's factors add to number A? 
You tell the program whether you want to enter 1 or 2 numbers. If you enter 1 number, it tests if the number has all the above properties, except for amicability. If you enter 2 numbers, it tests if both the numbers has all the above properties, and if they're amicable. Additionally, it shows their product, sum, difference, and division. 

Here's the code for it:



#include <stdlib.h>
#include <stdio.h>
#include <float.h>
#include <math.h>
#define DBL_EPSILON 2.2204460492503131e-16

int perfectNumber(int theNum) {
    int divisorList[100];
    int j = 0;
    int sumTotal = 0;

    for (int i = 1; i < theNum; i++) {
        if (theNum % i == 0) {
            divisorList[j] = i;
            j++;
            sumTotal += i;
        }
    }

    if (sumTotal == theNum) {
        return 1;
    }
    else {
        return 0;
    }
}

int primeNumber(int theNum) {
    int divisorList[100];
    int j = 0;
    int sumTotal = 0;

    for (int i = 1; i < theNum; i++) {
        if (theNum % i == 0) {
            divisorList[j] = i;
            j++;
            sumTotal += i;
        }
    }

     if (sumTotal == 1) {
        return 1;
    }
    else {
        return 0;
    }
}

int evenOdd(int theNum) {
    if (theNum % 2 == 0) {
        return 1;
    }
    else {
        return 0;
    }
}

int almostPerfectNumber(int theNum){
    int divisorList[100];
    int j = 0;
    int sumTotal = 0;

    for (int i = 1; i < theNum; i++) {
        if (theNum % i == 0) {
            divisorList[j] = i;
            j++;
            sumTotal += i;
        }
    }

    if (sumTotal + 1 == theNum) {
        return 1;
    }
    else {
        return 0;
    }
}

int fibonacciNumber(int theNum) {

    int fib1 = 1;
    int fib2 = 1;
    int tempFib;
    int fibList[3000];

    fibList[0] = fib1;
    fibList[1] = fib2;

    for(int i = 2; i < sizeof(fibList)/sizeof(int); i++){
        tempFib = fib1+fib2;
        fib1 = fib2;
        fib2 = tempFib;
        fibList[i] = tempFib;
    }

    for(int i = 0; i < sizeof(fibList)/sizeof(int); i++) {
        if (theNum == fibList[i]){
            return 1;
        }
    }
}

int lucasNumber(int theNum) {

    int fib1 = 1;
    int fib2 = 3;
    int tempFib;
    int fibList[3000];
    fibList[0] = fib1;
    fibList[1] = fib2;

    for(int i = 2; i < sizeof(fibList)/sizeof(int); i++){
        tempFib = fib1+fib2;
        fib1 = fib2;
        fib2 = tempFib;
        fibList[i] = tempFib;
    }

    for(int i = 0; i < sizeof(fibList)/sizeof(int); i++) {
        if (theNum == fibList[i]){
            return 1;
        }
    }
}

int perfectCube(int theNum) {

    double fTheNum = (double)theNum;

    if (theNum > 0){
        for (int i = 1; i < theNum; i++) {
                if (fabs(i-cbrt(fTheNum)) < DBL_EPSILON) {
                    return 1;
                    break;
                }
        }
    }
}

int perfectSquare(int theNum) {

    double fTheNum = (double)theNum;

    if (theNum > 0){
        for (int i = 1; i < theNum; i++) {
                if (fabs(i-sqrt(fTheNum)) < DBL_EPSILON) {
                    return 1;
                    break;
                }
        }
    }
}

void factors(int theNum) {
    if (theNum > 0) {
        int divisorLen = 100;
        int divisorList[divisorLen];
        int j = 0;
        int sumTotal = 0;

        printf("The factors of %d are: ", theNum);

        for (int i = 1; i < theNum; i++) {
            if (theNum % i == 0) {
                divisorList[j] = i;
                j++;
                sumTotal += i;
                printf("%d, ", i);
            }
        }
        if (theNum > 1){
            printf("and %d \n", theNum);
        }
        if (theNum == 1){
            printf("1");
        }
    }
}

int amicableNumbers(int theNum, int theNum2) {
    int divisorList[100];
    int j = 0;
    int sumTotal = 0;

    for (int i = 1; i < theNum; i++) {
        if (theNum % i == 0) {
            divisorList[j] = i;
            j++;
            sumTotal += i;
        }
    }

    int divisorList2[100];
    int k = 0;
    int sumTotal2 = 0;

    for (int i = 1; i < theNum2; i++) {
        if (theNum2 % i == 0) {
            divisorList[k] = i;
            k++;
            sumTotal2 += i;
        }
    }

    if ((sumTotal == theNum2) && (sumTotal2 == theNum)){
        return 1;
    }
    else {
        return 0;
    }
}

int triangularNumber(int theNum){
    int triangleLength = 10000;
    int triangularList[triangleLength];

    for(int i = 0; i < triangleLength; i++) {
        triangularList[i] = (i * (i + 1))/2;
    }

    for (int i = 0; i < triangleLength; i++) {
        if (theNum == triangularList[i]) {
            return 1;
            break;
        }
    }
    return 0;
}

int main() {

    int userNum = 1;
    int userNum2 = 0;
    int comparisons = 3;

    while (comparisons != 0){

        printf("Do you want the properties of one number or a pair of numbers (Enter 1 or 2, or 0 to quit.): \n");
        scanf("%d", &comparisons);

        if (comparisons == 0) {
            continue;
        }
        if (comparisons == 1) {
            printf("Enter a non-zero integer: \n");
            scanf("%d", &userNum);
            printf("\n");
        }
        else if (comparisons == 2) {
            printf("Enter a pair of non-zero integers, separated by a space: \n");
            scanf("%d %d", &userNum, &userNum2);
            printf("\n");
        }
        else {
            printf("You've entered an invalid response. Please try again. \n\n");
        }

        if ((comparisons == 1) || (comparisons == 2)) {
            if(userNum != 0) {
                if (evenOdd(userNum) == 1){
                    printf("%d is an even number. \n", userNum);
                }
                if (evenOdd(userNum) == 0) {
                    printf("%d is an odd number. \n", userNum);
                }
                if (perfectNumber(userNum) == 1) {
                    printf("%d is a perfect number. \n", userNum);
                }
                if (primeNumber(userNum) == 1) {
                    printf("%d is a prime number. \n", userNum);
                }
                if (primeNumber(userNum) == 0) {
                    printf("%d is a composite number. \n", userNum);
                }
                if (almostPerfectNumber(userNum) == 1) {
                    printf("%d is an almost perfect (deficient) number. \n", userNum);
                    printf("%d is a power of 2. \n", userNum);
                }
                if (fibonacciNumber(userNum) == 1) {
                    printf("%d is a Fibonacci number. \n", userNum);
                }
                if (lucasNumber(userNum) == 1) {
                    printf("%d is a Lucas number. \n", userNum);
                }
                if (perfectSquare(userNum) == 1) {
                    printf("%d is a perfect square. (The square root of %d is %d.) \n", userNum, userNum, (int)sqrt(userNum));
                }
                if (perfectCube(userNum) == 1) {
                    printf("%d is a perfect cube. (The cube root of %d is %d.) \n", userNum, userNum, (int)cbrt(userNum));
                }
                if (triangularNumber(userNum) == 1){
                    printf("%d is a triangular number. \n", userNum);
                }

                factors(userNum);

                printf("\n");
            }

            if (userNum2 != 0){

                if (evenOdd(userNum2) == 1){
                    printf("%d is an even number. \n", userNum2);
                }
                if (evenOdd(userNum2) == 0) {
                    printf("%d is an odd number. \n", userNum2);
                }
                if (perfectNumber(userNum2) == 1) {
                    printf("%d is a perfect number. \n", userNum2);
                }
                if (primeNumber(userNum2) == 1) {
                    printf("%d is a prime number. \n", userNum2);
                }
                if (primeNumber(userNum2) == 0) {
                    printf("%d is an composite number. \n", userNum2);
                }
                if (almostPerfectNumber(userNum2) == 1) {
                    printf("%d is an almost perfect (deficient) number. \n", userNum2);
                    printf("%d is a power of 2. \n", userNum2);
                }
                if (fibonacciNumber(userNum2) == 1) {
                    printf("%d is a Fibonacci number. \n", userNum2);
                }
                if (lucasNumber(userNum2) == 1) {
                    printf("%d is a Lucas number. \n", userNum2);
                }
                if (perfectSquare(userNum2) == 1) {
                    printf("%d is a perfect square. (The square root of %d is %d.) \n", userNum2, userNum2, (int)sqrt(userNum2));
                }
                if (perfectCube(userNum) == 1) {
                    printf("%d is a perfect cube. (The cube root of %d is %d.) \n", userNum2, userNum2, (int)cbrt(userNum2));
                }
                if (triangularNumber(userNum2) == 1){
                    printf("%d is a triangular number. \n", userNum2);
                }

                factors(userNum2);

                printf("\n");

                if (amicableNumbers(userNum, userNum2) == 1) {
                    printf("%d and %d are amicable numbers. \n\n", userNum, userNum2);
                }

                printf("The sum of %d and %d is %d. \n", userNum, userNum2, userNum+userNum2);
                printf("The difference between %d and %d is %d. \n", userNum, userNum2, userNum-userNum2);
                printf("The product of %d and %d is %d. \n", userNum, userNum2, userNum * userNum2);
                printf("The division of %d and %d is about %0.2f. \n", userNum, userNum2, (float)userNum/(float)userNum2);

            printf("\n\n");

            }
        }
    }
}

Thursday, September 12, 2019

Generating Animated SVG Tiles With C

So one assignment for my class is to essentially use C to generate SVGs and make a webpage using them, sort of. Me being me, that's too simple for me. I had to do more than that. The teacher suggested I animate the SVG. So I did so, sort of. I skimped on the complexity of animation for the interactivity of the webpage. Being HTML, I'm able to just embed it right here so you can play with it. It will be under the code.

Here's where I actually explain the code, line by line, for a more detailed explanation to the code:

And here's the actual C code if you want to compile it yourself and play with your own SVG implementations. HOWEVER DO NOT JUST COPY MY CODE AND USE IT IN YOUR OWN PROJECT IF YOU ARE COMING FROM THE VALENCIA C CLASS (you won't learn and I'm pretty sure the professor will know). Use just snippets of the code, like the bit that writes directly to the HTML file instead of you having to copy and paste the code. Seriously! I'm posting this to help y'all (and as a documentation of my progress in C), not to do your homework for you!

NOTE: You do not need an HTML file titled "animateSVG.html" pre-existing for the code to work. The code will generate it for you. You can change this to .txt if you prefer, but you can just open the HTML file with NotePad or other text editor if you'd like to view it.


#include <stdlib.h>
#include <stdio.h>


int main() {
    //This part will generate an html file to write to
    FILE *f = fopen("animateSVG.html", "w");
    if (f == NULL)
    {
        printf("Error opening file!\n");
        exit(1);
    }

    //For some reason my function had to be inside main; I assume it's because it needs to write to f and F
    //isn't really a passable value AFAIK

    void rectDraw(int rectHue) {
    fprintf(f,"<svg width=\"150\" height=\"150\">\n");
    fprintf(f,"\t<rect x=\"0\" y=\"0\" width=\"150\" height=\"150\" style=\"fill:hsl(%d, 100%%, 59%%);fill-opacity:1;\" />\n", rectHue);
    fprintf(f,"</svg>\n");
    //The rectHue is passed into the color value for the square. This will vary based on a following for loop.
    //fprintf is used instead of printf to write to the document.

}

    fprintf(f,"<!DOCTYPE html>\n<html>\n<body>\n");

    for(int i = 1; i <= 360; i++){ //360 seems to be the number of steps for a HSL spectrum.
        rectDraw(i);
    }

    fprintf(f,"<style>\n");
    fprintf(f,"\tsvg:hover{\n\t\ttransform: rotate(360deg);\n\t\topacity: 0;\n\t\ttransition-duration: 3s;\n");
    fprintf(f,"</style>");
    fprintf(f,"</body>\n</html>");


fclose(f);
printf("SVG saved to \"animateSVG.html\"\n"); //Verifies that the file did in fact write.
    }