Showing posts with label mathematics. Show all posts
Showing posts with label mathematics. Show all posts

## Tuesday 2 October 2012

### Binary, Hex, Octal and Decimal Conversion Under Linux

Base conversions are easy with linux CLI. No need of fancy GUI-based calculator to perform base conversions when there is our favorite linux terminal.

We will be using bc, a calculator language that supports arbitrary precision numbers with interactive execution of statements. We will exploit the pipelining feature of shell and will let the bc process our query to convert the numbers from one base to other.

### From binary to decimal

The syntax is obvious and we will follow the similar syntax for all the conversions. In this first example, we are converting the binary number 1101101 from input base binary to decimal(obase defaults to decimal unless specified).

samar@samar-Techgaun:~\$ echo "ibase=2;1101101" | bc
109

### From octal to decimal

samar@samar-Techgaun:~\$ echo "ibase=8;1101101" | bc
295489

samar@samar-Techgaun:~\$ echo "ibase=16;A1F3DF" | bc
10613727

### From N-base to decimal

All you need to do is provide the appropriate ibase value (eg. ibase=4 for 4-base to decimal conversion).

samar@samar-Techgaun:~\$ echo "ibase=16;A1F3DF" | bc
10613727

As seen in all the examples above, the conversion to decimal numbers does not require you to specify the obase as obase defaults to decimal. The same thing applies for ibase i.e. ibase defaults to decimal base by default as seen in the examples below.

Now lets try some conversion with decimal numbers as the input base.

### From decimal to binary

samar@samar-Techgaun:~\$ echo "obase=2;109" | bc
1101101

### From decimal to octal

samar@samar-Techgaun:~\$ echo "obase=8;295489" | bc
1101101

samar@samar-Techgaun:~\$ echo "obase=16;10613727" | bc
A1F3DF

### From decimal to N-base

All you need to do is provide the appropriate obase value (eg. obase=4 for decimal to 4-base conversion).

samar@samar-Techgaun:~\$ echo "obase=4;121" | bc
1321

Below are few more examples of base conversions to clarify the use of the command.

### From binary to octal

samar@samar-Techgaun:~\$ echo "ibase=2;obase=8;1111" | bc
17

samar@samar-Techgaun:~\$ echo "ibase=16;obase=2;AFBE" | bc
1010111110111110

I hope this is helpful ;-)

## Sunday 11 March 2012

### Finding Prime Factors Of A Number From Linux Terminal

Linux is awesome because of its powerful commanline interface from where you can do any tasks of varied complexities. I've been posting different command line tricks for system administrations & stuffs like that. But this time I am posting a little command line trick to find the prime factors of any number.

Linux provides a command factor that lets you find the prime factors of any number. So if you are into mathematics and working on prime factorizations, why worry? Just open the terminal and use the factor command.

This command takes any number of integer values as the argument and prints the prime factor of each of them. If no number is specified, it takes the value from standard input.

samar@Techgaun:~\$ factor 2056 1234567

More information on factor command: The factor command makes use of the Pollard Rho algorithm which is suitable for numbers with relatively small factors. It is not quite good for computing factors of large numbers whose factors are not small values.