Question

Write a FORTRAN program to solve linear congruence of the ax = b (mod m).

Write a FORTRAN program to solve linear congruence of the ax = b (mod m).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOLUTION :

Two integers are said to be congruent modulo an integer m if their difference is divisible by m. Thus a ≡ b (mod a) means (a−b)/m is an integer. For example, 3 ≡ 8 (mod 5) since [(3 – 8) / 5] = (−5/5) = −1 (an integer).
Congruences are similar to equations and linear congruences are similar to linear equations. The linear congruence
ax ≡ b (mod m)
has a solution if and only if (a, m) (the greatest common divider of a and m) divides b, i.e., (a, m)/b. Furthermore if x₀ is such a solution, then
x ≡ x₀ (mod m/(a, m)) is also a solution.
For example, solve the linear congruence

8x ≡ 4 (mod 6). (1)
By definition, (1) means

(8x – 4) / 6 = 3 integer
When x = 2, 8x − 4 = 12, and six divides twelve. Now, (8,6) = 2.
Hence any x satisfying
x ≡ 2 (mod 6/(8,6)), or ax − b = rm or ax − rm = b....(2)
Equation (2) is a linear Diophantine equation and is solvable for r and x if and only if (a, m) divides b.
The strategy in the presented computer program is as follows:
1) Read in values of a, b and m (< 10000).
2) Call a subroutine that solves the linear Diophantine equation described above.
3) Return to main program.
The program terminates when the value of M read in is less than, or equal to 0 5 BEAD (5,10) IA,M, IB
10 FORMAT (3I4)
IF (M.LE.O) GO TO 99
20 CALL GLDE(IA,M,IB,M1,N1,NA,INDEX)
IF (INDEX) 30,30,22
22 WRITE (6,25)
25 FORMAT (1HO,45HNO VALUE OF X SATISFIES
1 THE LINEAR CONGRUENCE)
GO TO 40
30 WRITE (6,35) IX
35 FORMAT (1HO.3HX =, I6, 32H SATISFIES THE LINEAR CONGRUENCE)
40 WRITE (6,44), IA,IB,M
44 FORMAT (1H , I6, 5HX (=),I16, 4H(MOD,I6,1H))
GO TO 5
99 STOP
END


SUBROUTINE GLDE(M,N,K,M1,N1,NA,INDEX)
C THIS SUBROUTINE RECEIVES INTEGERS FROM
C MAIN PROGRAM. FIRST, ANOTHER SUBROUTINE LDE IS CALLED, THAT FINDS THE GCD NA OF (N,M),
C AND DETERMINES VALUES M1 AND N1 THAT SATISFY THE LINEAD
C DIOPHANTINE EQUATION M*M1 + N * N1 = K.
C THE VALUE OF M1 RETURNED HAS THE LEAST
C POSSIBLE ABSOLUTE VALUE. INDEX IS SET
C TO 0 IF A SOLUTION EXISTS, TO / IF NOT,
C AND TO −1 IF THE VALUE RECEIVED FOR
C K IS, SAY, 30000. USE K = 30000 IF A
C SOLUTION TO THE EQUATION
C M*M1 + N*N1 = NA (GCD OF M AND N)
C IS DESIRED. NOTE: THE NUMBER 30000
C IS SOMEWHAT ARBITRARILY CHOSEN. IT IS
C ASSUMED THAT ONE WOULD NEVER WISH
C TO SOLVE THE EQUATION MX + NY = 30000.
CALL LDE (M,N,M1,N1,NA)
IF (K − 30000) 22,21,22
21 INDEX = −1
K = NA
GO TO 27
22 MULT = K/NA
IF (K − MULT * NA) 23,25,23
23 INDEX = 1
RETURN
25 Ml = M1 * MULT
N1 = N1 * MULT
27 MTEST = M1
ND = N/NA
MD = M/NA
MX = M1
30 MX = MX + ND
IF (IABS(MX) − LABS (M1)) 35,40,40
35 M1 = MX
N1 = N1 − MD
GO TO 30
40 IF (MTEST – M1) 65,45,65
45 MX = M1
50 MX = MX − ND
IF (IABS(MX) − IABS(M1)) 55,65,65
55 M1 = MX
N1 = N1 + MD
GO TO 50
65 INDEX = 0
RETURN
END


SUBROUTINE LDE(M,N,M1,N1,NA)

MO = 1

NO = 0

M1 = 0

N1 = 1

MA = IABS(M)

NA = IABS(N)

MS = M/MA
NS = N/NA
25 IQUOT = MA/NA
IREM = MA − NA * IQUOT
IF (IREM) 35,35,30
30 M2 = MO − IQUOT * M1
M2= NO − IQUOT * N1
MO = M1
NO= N1
M1 = M2
N1 = N2
MA = NA
NA = IREM
GO TO 25
35 M1 = MS * M1
N1 = NS * N1
RETURN
END   

Add a comment
Know the answer?
Add Answer to:
Write a FORTRAN program to solve linear congruence of the ax = b (mod m).
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT