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.
    }


One Of Several Rainbow Processing Wallpapers

So I have a personal project whereby I'm using Processing to create a series of rainbow-on-black wallpapers. One is a rainbow rain effect, some are flickering dots. Another is bouncing dots. They all use similar code but they look slightly different. My plan is to eventually create a wallpaper manager for a user to select the wallpaper they want and install it as their background image and/or lockscreen, all themed after this rainbow-on-black look that I went for.

Eventually I may get around to posting all the code for the wallpapers, if you'd like to download the APDE app and compile it for a wallpaper yourself.

The first line can be modified to increase or decrease the number of circles, and commenting out the b[i]=ballHueCycle; line can remove the color cycling to decrease the level of rainbow vomit that's present in the wallpaper.


Ball[] b = new Ball[100];

boolean randBoolX;
boolean randBoolY;


void setup() {
  fullScreen();
  colorMode(HSB, 100);

  for (int i = 0; i < b.length; i++) {
    float randBoolTestX = random(1, 100);
    float randBoolTestY = random(1, 100);

    if (randBoolTestX >= 50) {
      randBoolX = true;
    }
    if (randBoolTestX < 50) {
      randBoolX = false;
    }
    if (randBoolTestY >= 50) {
      randBoolY = true;
    }
    if (randBoolTestY < 50) {
      randBoolY = false;
    }
    b[i] = new Ball(false, random(0, 1), randBoolX, randBoolY, random(1, 100), random(1, width), random(1, height), random(1, 10), random(1, 10), random(10, 60));
  }
}
void draw() {
  background (0);

  for (int i = 0; i < b.length; i++) {

    b[i].ballDraw();
    b[i].ballBounce();
    b[i].ballMove();
    b[i].ballFade();
    b[i].ballTransform();
    b[i].ballHueCycle();


    if (b[i].ballTransp <= 0) {
      b[i] = new Ball(false, random(0, 1), randBoolX, randBoolY, random(1, 100), random(1, width), random(1, height), random(1, 10), random(1, 10), random(10, 60));
    }
  }
}

class Ball {

  float ballHue;
  float ballX;
  float ballY;

  float ballTransp = 1;
  float ballOffsetX = 0;
  float ballOffsetY = 0;

  float fadeSpeed;

  float randX;
  float randY;

  float ballDiam;
  boolean bounceDirX;
  boolean bounceDirY;
  boolean ballMaxTransp = false;
  float ballMaxSize = random(80, 120);
  Boolean ballMaxSizeBool;
  float ballEnlarge = 0;

  Ball(boolean tempBallMaxSizeBool, float tempFadeSpeed, Boolean tempBounceDirX, Boolean tempBounceDirY, float tempBallHue, float tempBallX, float tempBallY, float tempRandX, float tempRandY, float tempBallDiam) {
    ballHue = tempBallHue;
    ballX = tempBallX;
    ballY = tempBallY;
    randX = tempRandX;
    randY = tempRandY;
    ballDiam = tempBallDiam;
    bounceDirX = tempBounceDirX;
    bounceDirY = tempBounceDirY;
    fadeSpeed = tempFadeSpeed;
    ballMaxSizeBool = tempBallMaxSizeBool;
  }
  void ballDraw() {
    noStroke();
    fill(ballHue, 100, 100, ballTransp); 
    ellipse(ballX + ballOffsetX, ballY + ballOffsetY, ballDiam + ballEnlarge, ballDiam + ballEnlarge);
  }

  void ballBounce() {

    if (ballX + ballOffsetX + 0.5*ballDiam <= 0) {
      ballOffsetX += randX;
      bounceDirX = !bounceDirX;
    }
    if (ballX + ballOffsetX + 0.5*ballDiam >= width) {
      ballOffsetX -= randX;
      bounceDirX = !bounceDirX;
    }
    if (ballY + ballOffsetY + 0.5*ballDiam <= 0) {
      ballOffsetY += randY;
      bounceDirY = !bounceDirY;
    }
    if (ballY + ballOffsetY + 0.5*ballDiam >= height) {
      ballOffsetY -= randY;
      bounceDirY = !bounceDirY;
    }
  }
  void ballMove() {
    if (bounceDirX == true) {
      if (ballX + ballOffsetX + 0.5*ballDiam > 0 && ballX + ballOffsetX + 0.5*ballDiam < width) {
        ballOffsetX += randX;
      }
    }
    if (bounceDirY == true) {
      if (ballY + ballOffsetY + 0.5*ballDiam > 0 && ballY + ballOffsetY + 0.5*ballDiam < height) {
        ballOffsetY += randY;
      }
    }
    if (bounceDirX == false) {
      if (ballX + ballOffsetX + 0.5*ballDiam > 0 && ballX + ballOffsetX + 0.5*ballDiam < width) {
        ballOffsetX -= randX;
      }
    }
    if (bounceDirY == false) {
      if (ballY + ballOffsetY + 0.5*ballDiam > 0 && ballY + ballOffsetY + 0.5*ballDiam < height) {
        ballOffsetY -= randY;
      }
    }
  }
  void ballFade() {

    if (ballMaxTransp == false) {
      ballTransp++;
    }
    if (ballTransp >= 100) {
      ballMaxTransp = true;
    }
    if (ballMaxTransp == true) {
      ballTransp -= fadeSpeed;
    }
  }

  void ballTransform() {
    if (ballMaxSizeBool == false) {
      ballEnlarge += sin(0.1*randX);
    }
    if (ballDiam + ballEnlarge >= ballMaxSize) {
      ballMaxSizeBool = true;
      ballEnlarge -= sin(0.1*randX);
    }
    if (ballMaxSizeBool == true) {
      ballEnlarge -= sin(0.1*randX);
    }
    if (ballEnlarge <= 0) {
      ballMaxSizeBool = false;
    }
  }
  void ballHueCycle() {
    ballHue++;
    if (ballHue >100) {
      ballHue = 1;
    }
  }
}

Monday, August 26, 2019

An Averager In C

So I am taking a C course and I intentionally didn't want to look at the stuff early to see how fast I can pick up the course material relative to the class. Today is the first day. I've coded this. So now my questions are:

  1. How do I have it such that the outputted value is a float without having to define the input values as floats?
  2. How do I directly define elementArray[i] without having to use a placeholder?
  3. Is there a way to type in integers as "12" instead of having to write "12.0" if I do have to define them as floats in order for the output to be a float?
This is what a sample output looks like:



Here's what the code looks like. Note, this is built on Windows 8.1 in CodeBlocks.



#include <stdlib.h>
#include <stdio.h>
#define ISTRUE 1
#define ISFALSE 0

int main() {

    int elementCount;
    printf("How many elements do you want to average?\n");
    scanf("%d", &elementCount);
    printf("\n");

    int elementArray[elementCount];

    int veracity = ISTRUE;
    int i = 0;


    while (veracity == ISTRUE) {


        if (i < elementCount) {
            int placeholder;
            printf("Enter a value.\n");
            scanf("%d", &placeholder);
            elementArray[i] = placeholder;
            i++;

        }

        if (i >= elementCount){
            veracity = ISFALSE;
        }
    }
    int totalSum = 0;
    for (int i = 0; i < elementCount; i++) {
        totalSum = totalSum + elementArray[i];
    }
    printf("The average of your numbers is %d", totalSum/elementCount);

}