Advertisement
tsinclai2002

Project Euler #4 - Red Rum, Sir, is Murder

Jan 3rd, 2014
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.95 KB | None | 0 0
  1. #!/bin/bash
  2. # A quick little program to figure out the largest palindromic prime that is a product
  3. # of two 3-digit numbers.
  4.  
  5. A=900
  6. B=900
  7. C=0    
  8. PALINDROME=
  9. FACTOR1=
  10. FACTOR2=
  11.  
  12. # {Keep going until I tell you to stop}
  13. while true
  14. do
  15. {
  16.  
  17. # C = A * B
  18.  
  19. C=$(( $A * $B ))
  20. TEST1=`echo $C | rev` # Flip C and store it in TEST1
  21.  
  22. # echo $C $TEST1
  23.  
  24. # {Is C a palindrome AND bigger than PALINDROME?}
  25. # Yes: C becomes the new value for PALINDROME
  26.  
  27. if [ "$C" -eq "$TEST1" ]
  28. then
  29. {
  30.     #echo $C
  31.     PALINDROME=$C
  32.     FACTOR1=$A
  33.     FACTOR2=$B
  34. }
  35. fi
  36.  
  37. # No: Increment the value of B
  38.  
  39. let "B = B + 1"
  40.  
  41. # {Is B = 1000?}
  42. # Yes: reset B to 900 and add 1 to A
  43. if [ "$B" -eq 1000 ]
  44. then
  45. {
  46.     B=900
  47.     let "A = A + 1"
  48. }
  49. fi
  50.  
  51. # {Keep doing the above until A = 1000}
  52. if [ "$A" -eq 1000 ]
  53. then
  54.     break
  55. fi
  56.  
  57. } done
  58.  
  59. # Print out our answer
  60.  
  61. 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