From 60699af63f92f43cc4b4b9e3050fcdd2a8468281 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Wed, 10 Apr 2019 15:14:00 -0400 Subject: add opengl fronend, remove sdl frontend, refactor build system --- .../CEdev/examples/fileio_factorize/src/main.c | 75 ++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 frontends/calculator/CEdev/examples/fileio_factorize/src/main.c (limited to 'frontends/calculator/CEdev/examples/fileio_factorize/src') diff --git a/frontends/calculator/CEdev/examples/fileio_factorize/src/main.c b/frontends/calculator/CEdev/examples/fileio_factorize/src/main.c new file mode 100644 index 0000000..3be9992 --- /dev/null +++ b/frontends/calculator/CEdev/examples/fileio_factorize/src/main.c @@ -0,0 +1,75 @@ +#include +#include +#include +#include + +#include +#include +#include + +#include + +void prime_factors(unsigned int n); + +int primes[512]; +unsigned int total_primes = 0; + +/* Main Function */ +void main(void) { + /* Declare some variables */ + real_t *real_in; + list_t *list_out; + unsigned i; + int in; + + /* Clear the homescreen */ + os_ClrHome(); + + /* Get the answer variable */ + if (ti_RclVar(TI_REAL_TYPE, ti_Ans, &real_in)) return; + if ((in = os_RealToInt24(real_in)) < 1) return; + + /* Get the prime factors of the input */ + prime_factors((unsigned)in); + + /* Create a list to store the primes */ + if (!total_primes) return; + list_out = ti_MallocList(total_primes); // Same as ti_AllocList(total_primes, malloc) + + /* Write out the list of primes */ + for (i=0; iitems[i] = os_Int24ToReal(primes[i]); + } + + /* Set the new answer */ + ti_SetVar(TI_REAL_LIST_TYPE, ti_Ans, list_out); +} + +/* Store to an array all the prime numbers */ +void prime_factors(unsigned int n) { + unsigned int div, end; + + while (!(n % 2)) { + primes[total_primes++] = 2; + n /= 2; + } + + if (n == 1) return; + + div = 3; + end = sqrt(n); + + while (div <= end) { + if (!(n % div)) { + do { + primes[total_primes++] = div; + n /= div; + } while (!(n % div)); + if (n == 1) return; + end = sqrt(n); + } + div += 2; + } + + primes[total_primes++] = n; +} -- cgit v1.2.3-101-g0448