Loops – Arithmetic – Problem #5
IIT Kharagpur CS10001 / CS11001 Programming and Data Structures Lab Question Archive
Problem Statement
Note that (1)4^2mod23=16; (2)4^3mod23=18; (3)4^4mod23=3; (4)4^5mod23=12; (5)4^6mod23=2; (6)4^7mod23=8; (7)4^8mod23=9; (8)4^9mod23=13; (9)4^10mod23=6; and (10) 4^11mod23=1 but no power of 4 covers integers 4, 5, 7, 10, 11, 14, 15, 17, 19, 20, 21, 22 modulo 23. Here 23 is a prime number. However, (1)5^2mod23=2; (2)5^3mod23=10; (3)5^4mod23=4; (4)5^5mod23=20; (5)5^6mod23=8; (6)5^7mod23=17; (7)5^8mod23=16; (8)5^9mod23=11; (9)5^10mod23=9; (10)5^11mod23=22; (11)5^12mod23=18; (12)5^13mod23=21; (13)5^14mod23=13; (14)5^15mod23=19; (15)5^16mod23=3; (16)5^17mod23=15; (17)5^18mod23=6; (18)5^19mod23=7; (19)5^20mod23=12; (20)5^21mod23=14; (21)5^22mod23=1; We say that the ‘generator’ for prime number n=23 is r=5, where the generator r raised to integral powers generates all the 21 integers from 2 through 22, including r^1=5^1=5^23mod23=5. So, given a prime number n in the input, we require to compute the smallest integer r < n, called the ‘generator’, such that integral powers of r modulo n will span all integers 1 through n−1. Write a C program to compute the ‘generator’ of a given prime number n by first checking whether the given integer is a prime number and rejecting if it is not so.