Skip to content

Instantly share code, notes, and snippets.

@ph1ee
Last active November 6, 2022 09:39
Show Gist options
  • Save ph1ee/83e281d845c49c2232d56a39059bcfc4 to your computer and use it in GitHub Desktop.
Save ph1ee/83e281d845c49c2232d56a39059bcfc4 to your computer and use it in GitHub Desktop.
diffeq
#include <iostream>
#include <cstdint>
using namespace std;
int foo(int *coefficient, int coi, uint64_t n) {
if (n == 0) {
return 0;
}
coefficient[coi] = n % 10;
return 1 + foo(coefficient, coi + 1, n / 10);
}
int diff(int *coefficient, int num_of_digits) {
for (int i = 0; i < num_of_digits - 1; i++) {
coefficient[i] = (i + 1) * coefficient[i + 1];
}
return num_of_digits - 1;
}
void print(int *coefficient, int num_of_digits) {
for (int i = num_of_digits - 1; i >= 0; i--) {
if (coefficient[i] > 0) {
if (i < num_of_digits - 1) {
cout << "+";
}
cout << coefficient[i];
if (i > 0) {
cout << "X";
}
if (i > 1) {
cout << "^" << i;
}
}
}
cout << endl;
}
int main(int argc, char *argv[]) {
uint64_t n;
cin >> n;
int coefficient[64];
int coi = 0;
int num_of_digits = foo(coefficient, 0, n);
print(coefficient, num_of_digits);
num_of_digits = diff(coefficient, num_of_digits);
print(coefficient, num_of_digits);
return 0;
}
#include <iostream>
#include <cstdint>
using namespace std;
int foo(int *coefficient, int coi, uint64_t n) {
if (n == 0) {
return 0;
}
coefficient[coi] = n % 10;
return 1 + foo(coefficient, coi + 1, n / 10);
}
int diff(int *coefficient, int coi, int num_of_digits) {
if (num_of_digits == 0) {
return 0;
}
coefficient[coi] = (coi + 1) * coefficient[coi + 1];
return 1 + diff(coefficient, coi + 1, num_of_digits - 1);
}
int print(int *coefficient, int coi, const char *op) {
int printed = 0;
if (coi < 0) {
return 0;
}
if (coefficient[coi] > 0) {
cout << op;
if (coi > 0) {
if (coefficient[coi] > 1) {
cout << coefficient[coi];
}
cout << "X";
} else {
cout << coefficient[coi];
}
if (coi > 1) {
cout << "^" << coi;
}
printed = 1;
}
return printed + print(coefficient, coi - 1, "+");
}
int main(int argc, char *argv[]) {
uint64_t n;
cout << "Please input an integer: ";
cin >> n;
int coefficient[100];
int coi = 0;
int num_of_digits = foo(coefficient, 0, n);
cout << "The equation is ";
print(coefficient, num_of_digits - 1, "");
cout << endl;
num_of_digits = diff(coefficient, 0, num_of_digits - 1);
cout << "The equation after differential is ";
if (print(coefficient, num_of_digits - 1, "") == 0) {
cout << "0";
}
cout << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment