Listing 1

/* Find the Potency of an A for a given M
 * The C Users Journal
 * Creating Pseudo Random Numbers
 * Robert Fruit
 *
 ***********************************************************************/

int potency( long A, long M )
{
  long B;                         /* power of A                       */
  int i;

  A--;                            /* decrement A for potency test      */
  B = A;                          /* set initial power of A           */
  B = B % M;
  i = 0;
  while( (B != 0) && (i < 25) ){  /* loop while power of A not zero      */
    B = ( (A * B) % M );
    i++;
  }
  if( i >= 25 )                   /* if test failed report error         */
    i = -1;
  return( i ); 
}


/* Find the Potency of an A for maximum unsigned long M
 * The C Users Journal
 * Creating Pseudo Random Numbers
 * Robert Fruit
 *
 ***********************************************************************/

int potencyMaxM( long A )
{
  long B;
  int i;

  A--;
  B = A;
  i = 0;
  while( (B != 0) && (i < 25) ){
    B = A * B;
    i++;
  }
  if( i >= 25 )
    i = -1;
  return( i );
}


/*
/*   Test program to find the potency of A
 *
 ***********************************************************************/

#include <stdio.h>
#include <stdlib.h>

int potency( long A, long M );

void main( void )
{
  int pot;
  long A,M;

  printf("\n\n\nFind the potency for A values for a given M\n\n");
  printf("What is the M value --> ");
  scanf("%ld",&M);
  printf("\n\nWhat A value is to be tested --> ");
  scanf("%ld",&A);
  while( A != 0 ){
    pot = potency( A, M );
    printf("\nThe potency for A, %ld, given M, %ld, is %d",A,M,pot);
    printf("\n\nWhat A value is to be tested --> ");
    scanf("%ld",&A);
  }
}


Listing 2 includes two potency functions. The first one needs a M value
in order to calculate the potency. The second potency function assumes
that M has a value of 2^32, the maximum value for an unsigned long. The
testing program only uses the first potency function.