Numworks Epsilon  1.4.1
Graphing Calculator Operating System
arithmetic.cpp
Go to the documentation of this file.
1 #include <quiz.h>
2 #include <poincare.h>
3 #include <poincare/arithmetic.h>
4 #include <assert.h>
5 #include <utility>
6 
7 #if POINCARE_TESTS_PRINT_EXPRESSIONS
8 #include "../src/expression_debug.h"
9 #include <iostream>
10 using namespace std;
11 #endif
12 
13 using namespace Poincare;
14 
16 #if POINCARE_TESTS_PRINT_EXPRESSIONS
17  cout << "---- GCD ----" << endl;
18  cout << "gcd(" << a.approximate<float>();
19  cout << ", " << b.approximate<float>() << ") = ";
20 #endif
21  Integer gcd = Arithmetic::GCD(&a, &b);
22 #if POINCARE_TESTS_PRINT_EXPRESSIONS
23  cout << gcd.approximate<float>() << endl;
24 #endif
25  assert(gcd.isEqualTo(c));
26 }
27 
29 #if POINCARE_TESTS_PRINT_EXPRESSIONS
30  cout << "---- LCM ----" << endl;
31  cout << "lcm(" << a.approximate<float>();
32  cout << ", " << b.approximate<float>() << ") = ";
33 #endif
34  Integer lcm = Arithmetic::LCM(&a, &b);
35 #if POINCARE_TESTS_PRINT_EXPRESSIONS
36  cout << lcm.approximate<float>() << endl;
37 #endif
38  assert(lcm.isEqualTo(c));
39 }
40 
41 void assert_prime_factorization_equals_to(Integer a, int * factors, int * coefficients, int length) {
42  Integer outputFactors[100];
43  Integer outputCoefficients[100];
44 #if POINCARE_TESTS_PRINT_EXPRESSIONS
45  cout << "---- Primes factorization ----" << endl;
46  cout << "Decomp(" << a.approximate<float>() << ") = ";
47 #endif
48  Arithmetic::PrimeFactorization(&a, outputFactors, outputCoefficients, 10);
49 #if POINCARE_TESTS_PRINT_EXPRESSIONS
50  print_prime_factorization(outputFactors, outputCoefficients, 10);
51 #endif
52  for (int index = 0; index < length; index++) {
53  if (outputCoefficients[index].isEqualTo(Integer(0))) {
54  break;
55  }
56  /* Cheat: instead of comparing to integers, we compare their approximations
57  * (the relation between integers and their approximation is a surjection,
58  * however different integers are really likely to have different
59  * approximations... */
60  assert(outputFactors[index].approximate<float>() == Integer(factors[index]).approximate<float>());
61  assert(outputCoefficients[index].approximate<float>() == Integer(coefficients[index]).approximate<float>());
62  }
63 }
64 
65 QUIZ_CASE(poincare_arithmetic) {
69  assert_gcd_equals_to(Integer("1234567899876543456", true), Integer("234567890098765445678"), Integer(2));
70  assert_gcd_equals_to(Integer("45678998789"), Integer("1461727961248"), Integer("45678998789"));
72  assert_lcm_equals_to(Integer(-31), Integer(52), Integer(1612));
74  assert_lcm_equals_to(Integer("1234567899876543456", true), Integer("234567890098765445678"), Integer("144794993728852353909143567804987191584"));
75  assert_lcm_equals_to(Integer("45678998789"), Integer("1461727961248"), Integer("1461727961248"));
76  int factors0[5] = {2,3,5,79,1319};
77  int coefficients0[5] = {2,1,1,1,1};
78  assert_prime_factorization_equals_to(Integer(6252060), factors0, coefficients0, 5);
79  int factors1[3] = {3,2969, 6907};
80  int coefficients1[3] = {1,1,1};
81  assert_prime_factorization_equals_to(Integer(61520649), factors1, coefficients1, 3);
82  int factors2[3] = {2,5, 7};
83  int coefficients2[3] = {2,4,2};
84  assert_prime_factorization_equals_to(Integer(122500), factors2, coefficients2, 3);
85  int factors3[7] = {3,7,11, 13, 19, 3607, 3803};
86  int coefficients3[7] = {4,2,2,2,2,2,2};
87  assert_prime_factorization_equals_to(Integer("5513219850886344455940081"), factors3, coefficients3, 7);
88 }
T approximate() const
Definition: integer.cpp:479
void print_prime_factorization(Integer *outputFactors, Integer *outputCoefficients, int outputLength)
#define assert(e)
Definition: assert.h:9
void assert_prime_factorization_equals_to(Integer a, int *factors, int *coefficients, int length)
Definition: arithmetic.cpp:41
QUIZ_CASE(poincare_arithmetic)
Definition: arithmetic.cpp:65
c(generic_all_nodes)
void assert_gcd_equals_to(Integer a, Integer b, Integer c)
Definition: arithmetic.cpp:15
void assert_lcm_equals_to(Integer a, Integer b, Integer c)
Definition: arithmetic.cpp:28