Pseudocode for the Russian peasant method of binary

Just for fun… Here’s the pseudocode for the method of building a binary number from a decimal number, based on the Russian peasant method of multiplication:

function mybin(mydec) {

mybin = “”;

do while mydec > 0 {

if mydec is odd: {

mydec = mydec – 1;

mybin = “1” + mybin; }

else: mybin = “0” + mybin;

mydec = mydec / 2; }

return mybin; }

 

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.