/** * srexpmod.c * An implementation of the expmod function for SamR's simple math library. * * Copyright (c) 2022 Samuel A. Rebelsky. All rights reserved. * * This file is part of SRMath, SamR's simple math library. * * SRMath is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * SRMath is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with SRMath. If not, see . */ // +---------+--------------------------------------------------------- // | Headers | // +---------+ #include #include "srmath.h" #include "srtest.h" // +--------------------+---------------------------------------------- // | Exported Functions | // +--------------------+ long expmod (long x, long n, long m) { if (0 == n) { return 1; } else if (1 == (n % 2)) { return (x * expmod (x, n-1, m)) % m; } else { return expmod (x*x % m, n/2, m); } } // expmod