Skip to main content

Find the smallest binary digit multiple of given number

Here given code implementation process.

/*
    Java program for
    Find the smallest binary digit multiple of given number
*/
import java.util.HashSet;
import java.util.ArrayList;
public class Multiplier
{

    public void smallest01Multiple(int num)
    {
        // This is used to maintain order of resultant process
        ArrayList < String > process = new ArrayList < String > ();

        // This is used to determine unique remainder
        HashSet < Integer > record = new HashSet < Integer > ();

        // Declare useful  auxiliary and remainder variables
        String auxiliary = "";

        int remainder = -1;

        // Left most active digit
        process.add("1");

        // This loop execute until process element not empty
        // And remainder is not zero
        while (process.isEmpty() == false && remainder != 0)
        {
            // Get first element
            auxiliary = process.get(0);
           
            remainder = 0;

            // Calculate the mode of auxiliary value
            for(int i = 0; i < auxiliary.length(); ++i)
            {
                remainder = (remainder * 10) + (auxiliary.charAt(i) - '0');
                remainder = remainder % num;
            }
            if (remainder != 0 ) 
            {
                if(record.contains(remainder)==false)
                {
                    // Add calculate remainder
                    record.add(remainder);
                    // Add binary sequence
                    process.add(auxiliary + "0");
                    process.add(auxiliary + "1");
                }
              
            }
            // Remove first element
            process.remove(0);
           
        }
        // Display given number
        System.out.println(" Given number : "+num);
        // Display calculated result
        System.out.println(" Multiplier   : "+auxiliary);
    }
    public static void main(String[] args)
    {
        Multiplier task = new Multiplier();

        // Test
        task.smallest01Multiple(6);
        task.smallest01Multiple(52);
        task.smallest01Multiple(31);
    }
}

Output

 Given number : 6
 Multiplier   : 1110
 Given number : 52
 Multiplier   : 100100
 Given number : 31
 Multiplier   : 111011
// Include header file
#include <iostream>
#include <set>
#include <string>
#include <vector>

using namespace std;
class Multiplier
{
	public: void smallest01Multiple(int num)
	{
		// This is used to maintain order of resultant process
		vector < string > process;
		// This is used to determine unique remainder
		set < int > record;
		// Declare useful  auxiliary and remainder variables
		string auxiliary = "";
		int remainder = -1;
		// Left most active digit
		process.push_back("1");
		// This loop execute until process element not empty
		// And remainder is not zero
		while (process.empty() == false && remainder != 0)
		{
			// Get first element
			auxiliary = process.at(0);
			remainder = 0;
			// Calculate the mode of auxiliary value
			for (int i = 0; i < auxiliary.length(); ++i)
			{
				remainder = (remainder *10) + (auxiliary[i] - '0');
				remainder = remainder % num;
			}
			if (remainder != 0)
			{
				if (record.find(remainder) != record.end() == false)
				{
					// Add calculate remainder
					record.insert(remainder);
					// Add binary sequence
					process.push_back(auxiliary  +  "0");
					process.push_back(auxiliary  +  "1");
				}
			}
			// Remove first element
			process.erase(process.begin());
		}
		// Display given number
		cout << " Given number : " << num << endl;
		// Display calculated result
		cout << " Multiplier   : " << auxiliary << endl;
	}
};
int main()
{
	Multiplier *task = new Multiplier();
	// Test
	task->smallest01Multiple(6);
	task->smallest01Multiple(52);
	task->smallest01Multiple(31);
	return 0;
}

Output

 Given number : 6
 Multiplier   : 1110
 Given number : 52
 Multiplier   : 100100
 Given number : 31
 Multiplier   : 111011
// Include namespace system
using System;
using System.Collections.Generic;
public class Multiplier
{
	public void smallest01Multiple(int num)
	{
		// This is used to maintain order of resultant process
		List < string > process = new List < string > ();
		// This is used to determine unique remainder
		HashSet < int > record = new HashSet < int > ();
		// Declare useful  auxiliary and remainder variables
		String auxiliary = "";
		int remainder = -1;
		// Left most active digit
		process.Add("1");
		// This loop execute until process element not empty
		// And remainder is not zero
		while ((process.Count == 0) == false && remainder != 0)
		{
			// Get first element
			auxiliary = process[0];
			remainder = 0;
			// Calculate the mode of auxiliary value
			for (int i = 0; i < auxiliary.Length; ++i)
			{
				remainder = (remainder * 10) + (auxiliary[i] - '0');
				remainder = remainder % num;
			}
			if (remainder != 0)
			{
				if (record.Contains(remainder) == false)
				{
					// Add calculate remainder
					record.Add(remainder);
					// Add binary sequence
					process.Add(auxiliary + "0");
					process.Add(auxiliary + "1");
				}
			}
			// Remove first element
			process.RemoveAt(0);
		}
		// Display given number
		Console.WriteLine(" Given number : " + num);
		// Display calculated result
		Console.WriteLine(" Multiplier   : " + auxiliary);
	}
	public static void Main(String[] args)
	{
		Multiplier task = new Multiplier();
		// Test
		task.smallest01Multiple(6);
		task.smallest01Multiple(52);
		task.smallest01Multiple(31);
	}
}

Output

 Given number : 6
 Multiplier   : 1110
 Given number : 52
 Multiplier   : 100100
 Given number : 31
 Multiplier   : 111011
<?php
class Multiplier
{
	public	function smallest01Multiple($num)
	{
		// This is used to maintain order of resultant process
		$process = array();
		// This is used to determine unique remainder
		$record = array();
		// Declare useful  auxiliary and remainder variables
		$auxiliary = "";
		$remainder = -1;
		// Left most active digit
		$process[] = "1";
		// This loop execute until process element not empty
		// And remainder is not zero
		while (empty($process) == false && $remainder != 0)
		{
			// Get first element
			$auxiliary = $process[0];
			$remainder = 0;
			// Calculate the mode of auxiliary value
			for ($i = 0; $i < strlen($auxiliary); ++$i)
			{
				$remainder = ($remainder * 10) + 
                  (ord($auxiliary[$i]) - ord('0'));
				$remainder = $remainder % $num;
			}
			if ($remainder != 0)
			{
				if (in_array($remainder, $record, TRUE) == false)
				{
					// Add calculate remainder
					if (in_array($remainder, $record))
					{
						$record[] = $remainder;
					}
					// Add binary sequence
					$process[] = $auxiliary."0";
					$process[] = $auxiliary."1";
				}
			}
			// Remove first element
			array_splice($process, 0, 1);
		}
		// Display given number
		echo(" Given number : ".$num."\n");
		// Display calculated result
		echo(" Multiplier   : ".$auxiliary."\n");
	}
}

function main()
{
	$task = new Multiplier();
	// Test
	$task->smallest01Multiple(6);
	$task->smallest01Multiple(52);
	$task->smallest01Multiple(31);
}
main();

Output

 Given number : 6
 Multiplier   : 1110
 Given number : 52
 Multiplier   : 100100
 Given number : 31
 Multiplier   : 111011
/*
    Node JS program for
    Find the smallest binary digit multiple of given number
*/
class Multiplier
{
	smallest01Multiple(num)
	{
		// This is used to maintain order of resultant process
		var process = [];
		// This is used to determine unique remainder
		var record = new Set();
		// Declare useful  auxiliary and remainder variables
		var auxiliary = "";
		var remainder = -1;
		// Left most active digit
		process.push("1");
		// This loop execute until process element not empty
		// And remainder is not zero
		while ((process.Count == 0) == false && remainder != 0)
		{
			// Get first element
			auxiliary = process[0];
			remainder = 0;
			// Calculate the mode of auxiliary value
			for (var i = 0; i < auxiliary.length; ++i)
			{
				remainder = (remainder * 10) + 
                  (auxiliary.charAt(i).charCodeAt(0) - '0'.charCodeAt(0));
				remainder = remainder % num;
			}
			if (remainder != 0)
			{
				if (record.has(remainder) == false)
				{
					// Add calculate remainder
					record.add(remainder);
					// Add binary sequence
					process.push(auxiliary + "0");
					process.push(auxiliary + "1");
				}
			}
			// Remove first element
			process.splice(0, 1);
		}
		// Display given number
		console.log(" Given number : " + num);
		// Display calculated result
		console.log(" Multiplier   : " + auxiliary);
	}
}

function main()
{
	var task = new Multiplier();
	// Test
	task.smallest01Multiple(6);
	task.smallest01Multiple(52);
	task.smallest01Multiple(31);
}
main();

Output

 Given number : 6
 Multiplier   : 1110
 Given number : 52
 Multiplier   : 100100
 Given number : 31
 Multiplier   : 111011
#    Python 3 program for
#    Find the smallest binary digit multiple of given number
class Multiplier :
	def smallest01Multiple(self, num) :
		#  This is used to maintain order of resultant process
		process = []
		#  This is used to determine unique remainder
		record = set()
		#  Declare useful  auxiliary and remainder variables
		auxiliary = ""
		remainder = -1
		#  Left most active digit
		process.append("1")
		#  This loop execute until process element not empty
		#  And remainder is not zero
		while ((len(process) != 0) and remainder != 0) :
			#  Get first element
			auxiliary = process[0]
			remainder = 0
			i = 0
			#  Calculate the mode of auxiliary value
			while (i < len(auxiliary)) :
				remainder = (remainder * 10) + (ord(auxiliary[i]) - ord('0'))
				remainder = remainder % num
				i += 1
			
			if (remainder != 0) :
				if (remainder not in record ) :
					#  Add calculate remainder
					record.add(remainder)
					#  Add binary sequence
					process.append(auxiliary + "0")
					process.append(auxiliary + "1")
				
			
			#  Remove first element
			del process[0]
		
		#  Display given number
		print(" Given number : ", num)
		#  Display calculated result
		print(" Multiplier   : ", auxiliary)
	

def main() :
	task = Multiplier()
	#  Test
	task.smallest01Multiple(6)
	task.smallest01Multiple(52)
	task.smallest01Multiple(31)

if __name__ == "__main__": main()

Output

 Given number :  6
 Multiplier   :  1110
 Given number :  52
 Multiplier   :  100100
 Given number :  31
 Multiplier   :  111011
require 'set'
#    Ruby program for
#    Find the smallest binary digit multiple of given number
class Multiplier 
	def smallest01Multiple(num) 
		#  This is used to maintain order of resultant process
		process = []
		#  This is used to determine unique remainder
		record = []
		#  Declare useful  auxiliary and remainder variables
		auxiliary = ""
		remainder = -1
		#  Left most active digit
		process.push("1")
		#  This loop execute until process element not empty
		#  And remainder is not zero
		while (process.length != 0 && remainder != 0) 
		#  Get first element
		auxiliary = process[0]
		remainder = 0
		i = 0
		#  Calculate the mode of auxiliary value
		while (i < auxiliary.length) 
			remainder = (remainder * 10) + (auxiliary[i].ord - '0'.ord)
			remainder = remainder % num
			i += 1
		end

		if (remainder != 0) 
			if (record.include?(remainder) == false) 
				#  Add calculate remainder
				record.push(remainder)
				#  Add binary sequence
				process.push(auxiliary + "0")
				process.push(auxiliary + "1")
			end

		end

		#  Remove first element
		process.delete_at(0)
	end

	#  Display given number
	print(" Given number : ", num, "\n")
	#  Display calculated result
	print(" Multiplier   : ", auxiliary, "\n")
end

end

def main() 
	task = Multiplier.new()
	#  Test
	task.smallest01Multiple(6)
	task.smallest01Multiple(52)
	task.smallest01Multiple(31)
end

main()

Output

 Given number : 6
 Multiplier   : 1110
 Given number : 52
 Multiplier   : 100100
 Given number : 31
 Multiplier   : 111011
import scala.collection.mutable._;
/*
    Scala program for
    Find the smallest binary digit multiple of given number
*/
class Multiplier()
{
	def smallest01Multiple(num: Int): Unit = {
		// This is used to maintain order of resultant process
		var process: ArrayBuffer[String] = new ArrayBuffer[String]();
		// This is used to determine unique remainder
		var record = Set[Int]();
		// Declare useful  auxiliary and remainder variables
		var auxiliary: String = "";
		var remainder: Int = -1;
		// Left most active digit
		process += "1";
		// This loop execute until process element not empty
		// And remainder is not zero
		while ((process.size == 0) == false && remainder != 0)
		{
			// Get first element
			auxiliary = process(0);
			remainder = 0;
			var i: Int = 0;
			// Calculate the mode of auxiliary value
			while (i < auxiliary.length())
			{
				remainder = (remainder * 10) + 
                  (auxiliary.charAt(i).toInt - '0'.toInt);
				remainder = remainder % num;
				i += 1;
			}
			if (remainder != 0)
			{
				if (record.contains(remainder) == false)
				{
					// Add calculate remainder
					record.add(remainder);
					// Add binary sequence
					process += auxiliary + "0";
					process += auxiliary + "1";
				}
			}
			// Remove first element
			process.remove(0);
		}
		// Display given number
		println(" Given number : " + num);
		// Display calculated result
		println(" Multiplier   : " + auxiliary);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Multiplier = new Multiplier();
		// Test
		task.smallest01Multiple(6);
		task.smallest01Multiple(52);
		task.smallest01Multiple(31);
	}
}

Output

 Given number : 6
 Multiplier   : 1110
 Given number : 52
 Multiplier   : 100100
 Given number : 31
 Multiplier   : 111011
import Foundation;
/*
    Swift 4 program for
    Find the smallest binary digit multiple of given number
*/
class Multiplier
{
	func smallest01Multiple(_ num: Int)
	{
		// This is used to maintain order of resultant process
		var process = [String]();
		// This is used to determine unique remainder
		var record = Set<Int>()
		// Declare useful  auxiliary and remainder variables
		var auxiliary: String = "";
		var remainder: Int = -1;
		// Left most active digit
		process.append("1");
		// This loop execute until process element not empty
		// And remainder is not zero
		while ((process.count != 0) && remainder  != 0)
		{
			// Get first element
			auxiliary = process[0];
          	var temp = Array(auxiliary);
			remainder = 0;
			var i: Int = 0;
			// Calculate the mode of auxiliary value
			while (i < auxiliary.count)
			{
				remainder = (remainder * 10) + 
                  (Int(UnicodeScalar(String(temp[i]))!.value) - 
                   Int(UnicodeScalar(String("0"))!.value));
				remainder = remainder % num;
				i += 1;
			}
			if (remainder  != 0)
			{
				if (record.contains(remainder) == false)
				{
					// Add calculate remainder
					record.insert(remainder);
					// Add binary sequence
					process.append(auxiliary + "0");
					process.append(auxiliary + "1");
				}
			}
			// Remove first element
			process.remove(at: 0);
		}
		// Display given number
		print(" Given number : ", num);
		// Display calculated result
		print(" Multiplier   : ", auxiliary);
	}
}
func main()
{
	let task: Multiplier = Multiplier();
	// Test
	task.smallest01Multiple(6);
	task.smallest01Multiple(52);
	task.smallest01Multiple(31);
}
main();

Output

 Given number :  6
 Multiplier   :  1110
 Given number :  52
 Multiplier   :  100100
 Given number :  31
 Multiplier   :  111011
/*
    Kotlin program for
    Find the smallest binary digit multiple of given number
*/
class Multiplier
{
	fun smallest01Multiple(num: Int): Unit
	{
		// This is used to maintain order of resultant process
		var process = ArrayList<String>();
		// This is used to determine unique remainder
		var record: MutableSet <Int> = mutableSetOf <Int> ();
		// Declare useful  auxiliary and remainder variables
		var auxiliary: String = "";
		var remainder: Int = -1;
		// Left most active digit
		process.add("1");
		// This loop execute until process element not empty
		// And remainder is not zero
		while ((process.size != 0)  && remainder != 0)
		{
			// Get first element
			auxiliary = process[0];
			remainder = 0;
			var i: Int = 0;
			// Calculate the mode of auxiliary value
			while (i < auxiliary.length)
			{
				remainder = (remainder * 10) + 
                  (auxiliary.get(i).toInt() - '0'.toInt());
				remainder = remainder % num;
				i += 1;
			}
			if (remainder != 0)
			{
				if (record.contains(remainder) == false)
				{
					// Add calculate remainder
					record.add(remainder);
					// Add binary sequence
					process.add(auxiliary + "0");
					process.add(auxiliary + "1");
				}
			}
			// Remove first element
			process.removeAt(0);
		}
		// Display given number
		println(" Given number : " + num);
		// Display calculated result
		println(" Multiplier   : " + auxiliary);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Multiplier = Multiplier();
	// Test
	task.smallest01Multiple(6);
	task.smallest01Multiple(52);
	task.smallest01Multiple(31);
}

Output

 Given number : 6
 Multiplier   : 1110
 Given number : 52
 Multiplier   : 100100
 Given number : 31
 Multiplier   : 111011




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment