// example_int.cpp -- Int API examples // All code snippets are subroutines called from main() #include #include #include #include #include #include #include #include #include #include #include #include using namespace calx; // ============================================================ // Constructors // ============================================================ void demo_default_constructor() { std::cout << "--- default constructor ---" << std::endl; Int a; std::cout << "Int() = " << a << std::endl; } void demo_numeric_constructor() { std::cout << "--- numeric constructor ---" << std::endl; Int a = 42; Int b(INT64_MIN); // -2^63 Int c(UINT64_MAX); // 2^64 - 1 std::cout << "Int(42) = " << a << "\n"; std::cout << "Int(INT64_MIN) = " << b << "\n"; std::cout << "Int(UINT64_MAX) = " << c << "\n"; } void demo_string_constructor() { std::cout << "--- string constructor ---" << std::endl; // Explicit base Int dec("123456789012345678901234567890"); Int hex("DEADBEEF", 16); Int bin("11111111", 2); std::cout << "Int(\"123456789012345678901234567890\") = " << dec << "\n"; std::cout << "Int(\"DEADBEEF\", 16) = " << hex << "\n"; std::cout << "Int(\"11111111\", 2) = " << bin << "\n"; // base=0: auto-detect from prefix Int autoHex("0xDEADBEEF", 0); Int autoBin("0b11111111", 0); Int autoOct("0377", 0); std::cout << "Int(\"0xDEADBEEF\") = " << autoHex << "\n"; std::cout << "Int(\"0b11111111\") = " << autoBin << "\n"; std::cout << "Int(\"0377\") = " << autoOct << "\n"; } void demo_copy_move() { std::cout << "--- copy / move ---" << std::endl; Int a = 100; std::cout << "a = 100\n"; Int b = a; std::cout << "b = a (copy): a=" << a << " b=" << b << "\n"; Int c = std::move(a); std::cout << "c = move(a): a=" << a << " c=" << c << " (a is now empty)\n"; } // ============================================================ // Special value factory // ============================================================ void demo_special_values() { std::cout << "--- special values ---" << std::endl; Int inf = Int::infinity(); Int nan = Int::NaN(); std::cout << "infinity() = " << inf << "\n"; std::cout << "NaN() = " << nan << "\n"; std::cout << "Inf + 1 = " << (inf + Int(1)) << "\n"; std::cout << "Inf + (-Inf) = " << (inf + Int::negInfinity()) << "\n"; } // ============================================================ // NaN error classification (NumericError) // ============================================================ void demo_error_tracking() { std::cout << "--- special value propagation ---" << std::endl; // Division by zero produces Infinity (not a crash) std::cout << "42 / 0 = " << (Int(42) / Int(0)) << "\n"; // Infinity arithmetic std::cout << "Inf + 1 = " << (Int::infinity() + Int(1)) << "\n"; // Indeterminate form produces NaN std::cout << "Inf - Inf = " << (Int::infinity() - Int::infinity()) << "\n"; // NaN propagates through any operation Int nan = Int::infinity() - Int::infinity(); // NaN std::cout << "NaN + 42 = " << (nan + Int(42)) << "\n"; std::cout << "NaN * 100 = " << (nan * Int(100)) << "\n"; } // ============================================================ // fromState / fromRawWords // ============================================================ // fromState is an internal API, omitted from demo void demo_fromRawWords() { std::cout << "--- fromRawWords ---" << std::endl; // Build 2^64 + 1 directly from raw words std::vector w = {1, 1}; // w[0]=1 (low), w[1]=1 (high) Int n = Int::fromRawWords(w, +1); std::cout << "fromRawWords({1,1}) = 0x" << n.toString(16) << " (= 2^64 + 1)\n"; } // ============================================================ // State checking // ============================================================ void demo_state_checking() { std::cout << "--- state checking ---" << std::endl; Int a(42); Int b(-7); Int z(0); std::cout << std::boolalpha; std::cout << "Int(42).isPositive() = " << a.isPositive() << "\n"; std::cout << "Int(-7).isNegative() = " << b.isNegative() << "\n"; std::cout << "Int(0).isZero() = " << z.isZero() << "\n"; std::cout << "Int(42).isEven() = " << a.isEven() << "\n"; std::cout << "Int(-7).isOdd() = " << b.isOdd() << "\n"; std::cout << "NaN().isNaN() = " << Int::NaN().isNaN() << "\n"; } // ============================================================ // Type range checking // ============================================================ void demo_type_range() { std::cout << "--- type range ---" << std::endl; Int small(100); Int medium(40000); Int big("99999999999999999999"); std::cout << std::boolalpha; std::cout << "Int(100).fitsByte() = " << small.fitsByte() << " (100 <= 127)\n"; std::cout << "Int(100).fitsUByte() = " << small.fitsUByte() << " (100 <= 255)\n"; std::cout << "Int(40000).fitsShort() = " << medium.fitsShort() << " (40000 > 32767)\n"; std::cout << "Int(40000).fitsUShort() = " << medium.fitsUShort() << " (40000 <= 65535)\n"; std::cout << "Int(100).fitsInt() = " << small.fitsInt() << "\n"; std::cout << "Int(99...9).fitsInt64() = " << big.fitsInt64() << "\n"; std::cout << "Int(99...9).fitsUInt64() = " << big.fitsUInt64() << "\n"; } // ============================================================ // Arithmetic operators // ============================================================ void demo_arithmetic() { std::cout << "--- arithmetic ---" << std::endl; Int a("123456789012345678901234567890"); Int b("987654321098765432109876543210"); std::cout << "a = " << a << "\n"; std::cout << "b = " << b << "\n"; std::cout << "a + b = " << (a + b) << "\n"; std::cout << "a * b = " << (a * b) << "\n"; std::cout << "b / a = " << (b / a) << "\n"; std::cout << "b % a = " << (b % a) << "\n"; std::cout << "2 ^ 100 = " << (Int(2) ^ Int(100)) << "\n"; } // ============================================================ // Division variants -- IntOps // ============================================================ void demo_division_variants() { std::cout << "--- division variants ---" << std::endl; Int a(17), b(5); Int rem; Int q = IntOps::divmod(a, b, rem); std::cout << "17 / 5 = " << q << " remainder " << rem << "\n"; std::cout << "floorDiv(-7, 3) = " << IntOps::floorDiv(Int(-7), Int(3)) << " (C++ -7/3=-2, floor=-3)\n"; std::cout << "ceilDiv(7, 3) = " << IntOps::ceilDiv(Int(7), Int(3)) << "\n"; } // ============================================================ // Comparison operators // ============================================================ void demo_comparison() { std::cout << "--- comparison ---" << std::endl; Int a(100), b(200); std::cout << std::boolalpha; std::cout << "100 <=> 200 == less: " << (a <=> b == std::partial_ordering::less) << "\n"; std::cout << "100 == 200: " << (a == b) << "\n"; std::cout << "100 < 200: " << (a < b) << "\n"; std::cout << "100 >= 200: " << (a >= b) << "\n"; } // ============================================================ // Bitwise operators // ============================================================ void demo_bitwise_operators() { std::cout << "--- bitwise operators ---" << std::endl; Int a(0b1100); Int b(0b1010); std::cout << "a = 1100, b = 1010\n"; std::cout << "a & b = " << (a & b).toBinaryString() << "\n"; std::cout << "a | b = " << (a | b).toBinaryString() << "\n"; std::cout << "a ^ b = " << bitwiseXor(a, b).toBinaryString() << "\n"; std::cout << "a << 4 = " << (a << 4).toBinaryString() << "\n"; std::cout << "~(-1) = " << (~Int(-1)) << " (two's complement)\n"; } // ============================================================ // Bit manipulation // ============================================================ void demo_bit_manipulation() { std::cout << "--- bit manipulation ---" << std::endl; Int a(255); // 0b11111111 std::cout << "Int(255).bitLength() = " << a.bitLength() << "\n"; std::cout << "Int(255).countTrailingZeros() = " << a.countTrailingZeros() << "\n"; Int b(256); // 0b100000000 std::cout << "Int(256).countTrailingZeros() = " << b.countTrailingZeros() << "\n"; std::cout << "hammingDistance(255, 256) = " << Int::hammingDistance(a, b) << "\n"; } // ============================================================ // Numeric conversion // ============================================================ void demo_numeric_conversion() { std::cout << "--- numeric conversion ---" << std::endl; Int a(42); std::cout << "Int(42).toInt() = " << a.toInt() << "\n"; std::cout << "Int(42).toInt64() = " << a.toInt64() << "\n"; std::cout << "Int(42).toDouble() = " << a.toDouble() << "\n"; } // ============================================================ // String conversion // ============================================================ void demo_string_conversion() { std::cout << "--- string conversion ---" << std::endl; Int a(255); std::cout << "Int(255).toString() = " << a.toString() << "\n"; std::cout << "Int(255).toString(16) = " << a.toString(16) << "\n"; std::cout << "Int(255).toString(2) = " << a.toString(2) << "\n"; std::cout << "Int(255).toString(8) = " << a.toString(8) << "\n"; } void demo_fromString() { std::cout << "--- fromString ---" << std::endl; Int a("DEADBEEF", 16); std::cout << "Int(\"DEADBEEF\", 16) = " << a << "\n"; } void demo_std_format() { std::cout << "--- std::format ---" << std::endl; Int a(255); std::cout << "a = Int(255)\n"; std::cout << "format(\"{}\", a) = " << std::format("{}", a) << "\n"; std::cout << "format(\"{:x}\", a) = " << std::format("{:x}", a) << "\n"; std::cout << "format(\"{:#X}\", a)= " << std::format("{:#X}", a) << "\n"; std::cout << "format(\"{:b}\", a) = " << std::format("{:b}", a) << "\n"; std::cout << "format(\"{:+d}\",a) = " << std::format("{:+d}", a) << "\n"; } // ============================================================ // Digit count and logarithm // ============================================================ void demo_digit_log() { std::cout << "--- digit / log ---" << std::endl; Int a("123456789012345678901234567890"); std::cout << "a = " << a << "\n"; std::cout << "a.digitCount() = " << a.digitCount() << "\n"; std::cout << "a.sizeInBase() = " << a.sizeInBase() << "\n"; std::cout << "a.ilog2() = " << a.ilog2() << "\n"; std::cout << "a.ilog10() = " << a.ilog10() << "\n"; } // ============================================================ // GCD / LCM // ============================================================ void demo_gcd() { std::cout << "--- gcd ---" << std::endl; std::cout << "gcd(48, 36) = " << gcd(Int(48), Int(36)) << "\n"; } void demo_lcm() { std::cout << "--- lcm ---" << std::endl; std::cout << "lcm(12, 18) = " << lcm(Int(12), Int(18)) << "\n"; } void demo_extended_gcd() { std::cout << "--- extended gcd ---" << std::endl; Int a(35), b(15), x, y; Int g = IntGCD::extendedGcd(a, b, x, y); std::cout << "extendedGcd(35, 15): gcd=" << g << ", x=" << x << ", y=" << y << "\n"; std::cout << "verify: 35*" << x << " + 15*" << y << " = " << (a * x + b * y) << "\n"; } // ============================================================ // Exponentiation // ============================================================ void demo_pow() { std::cout << "--- pow ---" << std::endl; Int a(2); std::cout << "2^100 = " << pow(a, 100u) << "\n"; } void demo_powMod() { std::cout << "--- powMod ---" << std::endl; Int base(2), exp(100), mod(1000000007); std::cout << "powMod(2, 100, 1000000007) = " << powMod(base, exp, mod) << "\n"; } // ============================================================ // Modular arithmetic // ============================================================ void demo_modular_mod() { std::cout << "--- IntModular::mod ---" << std::endl; std::cout << "mod(-7, 5) = " << IntModular::mod(Int(-7), Int(5)) << " (C++ % gives -2)\n"; std::cout << "mod(7, 5) = " << IntModular::mod(Int(7), Int(5)) << "\n"; } void demo_inverseMod() { std::cout << "--- IntModular::inverseMod ---" << std::endl; std::cout << "inverseMod(3, 7) = " << IntModular::inverseMod(Int(3), Int(7)) << " (3*5=15=1 mod 7)\n"; std::cout << "inverseMod(2, 5) = " << IntModular::inverseMod(Int(2), Int(5)) << " (2*3=6=1 mod 5)\n"; } // ============================================================ // Primality testing // ============================================================ void demo_isProbablePrime() { std::cout << "--- isProbablePrime ---" << std::endl; Int mersenne = pow(Int(2), 127u) - Int(1); std::cout << "2^127 - 1 = " << mersenne << "\n"; std::cout << "isProbablePrime(2^127-1) = " << std::boolalpha << isProbablePrime(mersenne) << "\n"; } void demo_IntPrime() { std::cout << "--- IntPrime ---" << std::endl; std::cout << "nextPrime(100) = " << IntPrime::nextPrime(Int(100)) << "\n"; std::cout << "prevPrime(100) = " << IntPrime::prevPrime(Int(100)) << "\n"; } // ============================================================ // Combinatorics // ============================================================ void demo_factorial() { std::cout << "--- factorial ---" << std::endl; std::cout << "20! = " << IntCombinatorics::factorial(Int(20)) << "\n"; std::cout << "100! = " << IntCombinatorics::factorial(Int(100)) << "\n"; } void demo_doubleFactorial() { std::cout << "--- doubleFactorial ---" << std::endl; std::cout << "6!! = " << IntCombinatorics::doubleFactorial(Int(6)) << " (6*4*2)\n"; std::cout << "7!! = " << IntCombinatorics::doubleFactorial(Int(7)) << " (7*5*3*1)\n"; } void demo_binomial() { std::cout << "--- binomial ---" << std::endl; std::cout << "C(10,3) = " << IntCombinatorics::binomial(Int(10), Int(3)) << "\n"; std::cout << "C(100,50) = " << IntCombinatorics::binomial(Int(100), Int(50)) << "\n"; } // ============================================================ // Square root and nth root // ============================================================ void demo_sqrt() { std::cout << "--- sqrt ---" << std::endl; std::cout << "sqrt(1000000) = " << IntSqrt::sqrt(Int(1000000)) << "\n"; std::cout << "sqrt(2) = " << IntSqrt::sqrt(Int(2)) << " (integer part)\n"; } void demo_sqrtRem() { std::cout << "--- sqrtRem ---" << std::endl; Int rem; Int s = IntSqrt::sqrtRem(Int(50), rem); std::cout << "sqrtRem(50): sqrt=" << s << ", rem=" << rem << " (50 = 7^2 + 1)\n"; // 50 = 7^2 + 1 } void demo_isSquare() { std::cout << "--- isSquare ---" << std::endl; Int s; std::cout << std::boolalpha; std::cout << "isSquare(144) = " << IntSqrt::isSquare(Int(144), &s) << " sqrt=" << s << "\n"; std::cout << "isSquare(145) = " << IntSqrt::isSquare(Int(145)) << "\n"; } void demo_nthRoot() { std::cout << "--- nthRoot ---" << std::endl; std::cout << "nthRoot(1000000, 3) = " << IntSqrt::nthRoot(Int(1000000), 3) << " (= 100)\n"; } void demo_nthRootRem() { std::cout << "--- nthRootRem ---" << std::endl; Int rem; Int r = IntSqrt::nthRootRem(Int(1000), 3, rem); std::cout << "nthRootRem(1000, 3): root=" << r << ", rem=" << rem << " (1000 = 10^3 + 0)\n"; // 1000 = 10^3 + 0 } // ============================================================ // Other utilities // ============================================================ void demo_abs() { std::cout << "--- abs ---" << std::endl; std::cout << "abs(-42) = " << abs(Int(-42)) << "\n"; } void demo_removeFactor() { std::cout << "--- removeFactor ---" << std::endl; auto [quotient, count] = removeFactor(Int(72), Int(2)); std::cout << "72 = " << quotient << " x 2^" << count << std::endl; // -> 72 = 9 x 2^3 auto [q2, c2] = removeFactor(Int(1000), Int(10)); std::cout << "1000 = " << q2 << " x 10^" << c2 << std::endl; // -> 1000 = 1 x 10^3 } void demo_isDivisible() { std::cout << "--- isDivisible ---" << std::endl; std::cout << std::boolalpha; std::cout << "12.isDivisible(3) = " << Int(12).isDivisible(Int(3)) << "\n"; std::cout << "13.isDivisible(3) = " << Int(13).isDivisible(Int(3)) << "\n"; } void demo_isCongruent() { std::cout << "--- isCongruent ---" << std::endl; std::cout << std::boolalpha; std::cout << "17.isCongruent(2, 5) = " << Int(17).isCongruent(Int(2), Int(5)) << " (17 = 2 mod 5)\n"; } void demo_swap() { std::cout << "--- swap ---" << std::endl; Int a(10), b(20); std::cout << "before: a=" << a << " b=" << b << "\n"; swap(a, b); std::cout << "after: a=" << a << " b=" << b << "\n"; } // ============================================================ // main // ============================================================ int main() { std::cout << "=== calx Int API examples ===" << std::endl; // Constructors demo_default_constructor(); demo_numeric_constructor(); demo_string_constructor(); demo_copy_move(); // Special values demo_special_values(); demo_error_tracking(); // demo_fromState omitted (internal API) demo_fromRawWords(); // State checking demo_state_checking(); demo_type_range(); // Arithmetic demo_arithmetic(); demo_division_variants(); demo_comparison(); // Bitwise demo_bitwise_operators(); demo_bit_manipulation(); // Conversion demo_numeric_conversion(); demo_string_conversion(); demo_fromString(); demo_std_format(); demo_digit_log(); // GCD / LCM demo_gcd(); demo_lcm(); demo_extended_gcd(); // Exponentiation demo_pow(); demo_powMod(); // Modular arithmetic demo_modular_mod(); demo_inverseMod(); // Primality testing demo_isProbablePrime(); demo_IntPrime(); // Combinatorics demo_factorial(); demo_doubleFactorial(); demo_binomial(); // Square root and nth root demo_sqrt(); demo_sqrtRem(); demo_isSquare(); demo_nthRoot(); demo_nthRootRem(); // Utilities demo_abs(); demo_removeFactor(); demo_isDivisible(); demo_isCongruent(); demo_swap(); return 0; }