Advertisement
Guest User

backtrack recursive number to word algorithm

a guest
Jan 17th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. so the idea is to simplify the number structure and to do this we need to turn our number into a string so that we can easily split the magnitudes out
  2.  
  3. once our number is a string we get the string to return true for this conditional statement (number.length % 3 == 0)
  4. from there we split the string into equal parts of three so for an example lets take 6984321847654131
  5. we'll split it to look like (006)(984)(321)(847)(654)(131)
  6. from here we can use our understanding on our numbering system of magnitude
  7. (006) = quadrillion
  8. (984) = trillions
  9. (321) = billions
  10. (847) = millions
  11. (654) = thousands
  12. (131) = hundreds and lower
  13.  
  14. so the idea is to take groups of three which in our numbering system will always have the structure of (hundreds + tens/or ones)+ magnitude compound
  15. example:
  16. (for the trillions magnitude) (984)trillion will look like "nine hundred and eighty four trillion"
  17.  
  18. so to turn our number into a word we can use a back tracking recursive algorithm (this is honestly here just to satisfy the program requirement and to get my rocks off to another world)
  19. so we push our lower magnitude numbers into a lifo structure which will look like this
  20. (006)
  21. (984)
  22. (321)
  23. (847)
  24. (654)
  25. (131)
  26. and once we have this pushed onto the stack we write the number to a string and += the number word together and pop it off the stack
  27. which gives us the result of
  28.  
  29. six quadrillion nine hundred eighty four trillion three hundred twenty one billion eight hundred fourty seven million six hundred fifty four thousand one hundred and thirty one
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement