diff --git a/pset5/3-qualify.c b/pset5/3-qualify.c new file mode 100644 index 0000000..5f8eec3 --- /dev/null +++ b/pset5/3-qualify.c @@ -0,0 +1,38 @@ +#include + +// Function prototypes +int get_sum(int n); + +int main(void) { + // Initialize variables + int n, m; + scanf("%i%i", &n, &m); + + // Brute force answer + for (int i = 1; i < n; i++) { + if (i / m == get_sum(i)) { + printf("%i\n", i); + } + } + + return 0; +} + +int get_sum(int n) { + // Tabulate nums + int length = 0; + int nums[6]; + int tmp = n; + while (tmp > 0) { + nums[length] = tmp % 10; + tmp /= 10; + length++; + } + + // Calculate sum + int sum = 0; + for (int i = 0; i < length; i++) { + sum += nums[i] * nums[i]; + } + return sum; +} diff --git a/pset5/4-isomorph.c b/pset5/4-isomorph.c new file mode 100644 index 0000000..506038d --- /dev/null +++ b/pset5/4-isomorph.c @@ -0,0 +1,56 @@ +#include + +// Function prototypes +int test(int n); +void tabulate(int *num, int n); + +int main(void) { + // Initialize variables + int n; + scanf("%d", &n); + + // Determine if it is a isomorph + if (n > 0 && n <= 10000) { + if (test(n)) { + printf("Yes\n"); + } else { + printf("No\n"); + } + } else { + printf("%i out of range\n", n); + } + + return 0; +} + +int test(int n) { + int pow = n * n; + int num1[6], num2[12]; + + // Get the length of n + int length = 0; + int tmp = n; + while (tmp > 0) { + tmp /= 10; + length++; + } + + // Tabulate them + tabulate(&num1[0], n); // Or: num1 + tabulate(&num2[0], pow); + + for (int i = 0; i < length; i++) { + if (num1[i] != num2[i]) { + return 0; // False + } + } + return 1; // True +} + +void tabulate(int *num, int n) { + while (n > 0) { + *num = n % 10; + n /= 10; + num++; + } +} diff --git a/pset5/5-matrix.c b/pset5/5-matrix.c new file mode 100644 index 0000000..ab261f6 --- /dev/null +++ b/pset5/5-matrix.c @@ -0,0 +1,32 @@ +#include +#include + +// Function prototypes +int checkf(float f); + +int main(void) { + // Initialize variables + float m, n; + scanf("%f%f", &m, &n); + + int a; + float bf, cf; + for (a = 0; a <= (int)m; a++) { + cf = (float)a + (n / 2) - (m * 2); + if (checkf(cf) == 1) { + bf = ((n / 2) - m - cf * 2); + if (checkf(bf) == 1) { + printf("%i %i %i\n", a, (int)bf, (int)cf); + } + } + } + return 0; +} + +int checkf(float f) { + if (floorf(f) == f && f >= 0) { + return 1; // True + } else { + return 0; // False + } +}