Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # A quick little program to figure out the largest palindromic prime that is a product
- # of two 3-digit numbers.
- A=900
- B=900
- C=0
- PALINDROME=
- FACTOR1=
- FACTOR2=
- # {Keep going until I tell you to stop}
- while true
- do
- {
- # C = A * B
- C=$(( $A * $B ))
- TEST1=`echo $C | rev` # Flip C and store it in TEST1
- # echo $C $TEST1
- # {Is C a palindrome AND bigger than PALINDROME?}
- # Yes: C becomes the new value for PALINDROME
- if [ "$C" -eq "$TEST1" ]
- then
- {
- #echo $C
- PALINDROME=$C
- FACTOR1=$A
- FACTOR2=$B
- }
- fi
- # No: Increment the value of B
- let "B = B + 1"
- # {Is B = 1000?}
- # Yes: reset B to 900 and add 1 to A
- if [ "$B" -eq 1000 ]
- then
- {
- B=900
- let "A = A + 1"
- }
- fi
- # {Keep doing the above until A = 1000}
- if [ "$A" -eq 1000 ]
- then
- break
- fi
- } done
- # Print out our answer
- echo "$PALINDROME is the largest palindromic number that is a product of two three-digit numbers ($FACTOR1 and $FACTOR2)."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement