Advertisement
james_lynch

collatzConjecture.bat

Jul 31st, 2014
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 1.26 KB | None | 0 0
  1. REM Defines the variables 'change' and 'count'
  2. @echo off
  3. cls
  4. set /a change=0
  5. set /a count=0
  6.  
  7. REM Asks for input. The script will loop back
  8. REM to this part in later sections of the code.
  9. :loop
  10. set /p "input=Input a number: "
  11. set /a change=input
  12. goto :skip
  13.  
  14. REM Sets the variable 'input' to the same value
  15. REM as 'change'.
  16. :numtest
  17. if %change%==0 (
  18. goto :skip
  19. )
  20. set /a input=change
  21.  
  22. :skip
  23. set /a count=count+1
  24. set /a "number=%input%" 2>nul
  25.  
  26. REM Makes sure that the number is valid.
  27. if "%input%" NEQ "%number%" (
  28. cls
  29. Echo Please enter a valid number! &Echo.&Echo.
  30. goto :loop
  31. )
  32. Set /a Even=number%%2
  33.  
  34. REM Mathematical equations depending on whether
  35. REM the input was even or odd.
  36. if %Even% EQU 0 (
  37. set /a answer=number/2
  38. ) else (
  39. set /a answer=number*3
  40. set /a answer=answer+1
  41. )
  42. if %answer%==1 (
  43. goto :print
  44. ) else (
  45. goto :check
  46. )
  47.  
  48. REM Prints the number that you input at the start, and
  49. REM the number of calculations made.
  50. :print
  51. echo Your input (%change%) returned as 1.
  52. echo A total of (%count%) calculations were made.
  53. set /a change=change+1
  54. set /a count=0
  55. pause
  56. Echo.
  57. goto :numtest
  58.  
  59. REM Sets 'input' to the value of 'answer' and repeats
  60. REM the calculations over and over until 'answer' is 1.
  61. :check
  62. set /a input=answer
  63. goto :skip
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement