Tag Archives: UNIX

Small script to calculate future value

Heres a small script I wrote to calculate the future value.

#!/usr/bin/env python

# This script calculate the future value

iv = float(raw_input("Enter initial value: "));
rate = float(raw_input("Enter interest rate: "));
times = int(raw_input("Enter the amount of years: "));

fv = iv*((1 + (rate/100))**times)

print "Final amount is: %.2f." %fv

Project Euler number 92

Below is the solution to Project Euler problem 92. Its written in python and takes about 10 minutes to reach the solution depending on your hardware. There are some comments in the code to know what the program is doing.

#!/usr/bin/env python

number_89 = 0

for z in range(10000000, 1, -1):

        def chain(z):
                var = z
                chaindigit = 0
                while (chaindigit != 1 or chaindigit != 89):
                        # Transform the number into a string
                        list =  [int(i) for i in str(var)]
                        # Multiply each list member 
                        chain = [x * y for x, y in zip(list, list)]
                        # Add the remaining list members
                        chaindigit = sum(chain)
                        var = chaindigit
                        # Check whether it ends in 89 or 1 
                        if (chaindigit == 1 or chaindigit == 89):
                                # If it ends in 89 increment the value of number_89
                                if (chaindigit == 89):
                                        global number_89
                                        number_89 = number_89 + 1
                                break
        chain(z)

print "There are", number_89,"chain numbers that end in 89."

Project Euler 12

So I finished problem 12 from Project Euler. I made it via a brute force attack in Perl.

#!/usr/bin/env perl

use warnings;
use strict;

my $j = 0;
for (my $x=1; $x <= 1000000; $x++) {
        $j = $j + $x;
        my $dividend = 0;
        print "j is $j, x is $x and dividend is $dividend.\n";
        for (my $z=1; $z <= ($j + 1); $z++) {
                if ($j%$z == 0) {
                        $dividend = $dividend + 1;
                        print "$z is dividend of $j and $j has dividend $dividend.\n"; 
                        if ($dividend > 501) {
                                print "$z is dividend of $j and $j has dividend $dividend.\n"; 
                                exit; }
                                }
                        }
                }

and with some logic in Python. The python script can be divided in three part, first we find the triangle number, then we calculate the numbers that factor the triangle number and place it into an array and the last step would be to calculate how many divisors the triangle number has.

#!/usr/bin/python 

import math

# Start the triangle by one
triangle = 0
for x in range(1, 100000):
        triangle = x*(x+1)/2
        #print "triangles is ", triangle

# We factor the triangle into prime numbers
        def primefactors(x):
                factorlist=[]
                loop=2
                while loop<=x:
                        if x%loop==0:
                                x/=loop
                                factorlist.append(loop)
                        else:
                                loop+=1
                return factorlist
        #print primefactors(triangle)

# We calculate the number of divisors of the triangle number
        divisor = 1
        for z in set(primefactors(triangle)):
                #print "{0}\t{1}".format(z,primefactors(triangle).count(z))
                divisor = (primefactors(triangle).count(z) + 1) * divisor
                #print "Triangle", triangle," has ", divisor," divisors."
                if (divisor > 500):
                        print "Triangle", triangle," has ", divisor," divisors."
                        quit()

Reference:
1) Integer Factorization
2) Triangular numbers
3) Number of divisors of a number

Setting replication MySQL version 5.5

So the other day I performed an upgrade of MySQL on a linode and notice that it wont start if I kept my old my.cnf file.

[11:47:06] user@linode1: ~ $ echo 'SHOW VARIABLES LIKE "%version%";' | mysql -u username -ppassword | grep innodb
innodb_version  5.5.31
[11:47:20] user@linode1: ~ $ 
131022 08:17:56 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
131022  8:17:56 [ERROR] An old style --language value with language specific part detected: /usr/share/mysql/english/
131022  8:17:56 [ERROR] Use --lc-messages-dir without language specific part instead.
131022  8:17:56 [Note] Plugin 'FEDERATED' is disabled.
131022  8:17:56 InnoDB: The InnoDB memory heap is disabled
131022  8:17:56 InnoDB: Mutexes and rw_locks use GCC atomic builtins
131022  8:17:56 InnoDB: Compressed tables use zlib 1.2.7
131022  8:17:56 InnoDB: Using Linux native AIO
131022  8:17:56 InnoDB: Initializing buffer pool, size = 128.0M
131022  8:17:56 InnoDB: Completed initialization of buffer pool
131022  8:17:56 InnoDB: highest supported file format is Barracuda.
131022  8:17:56  InnoDB: Waiting for the background threads to start
131022  8:17:57 InnoDB: 5.5.31 started; log sequence number 1678395
131022  8:17:57 [ERROR] /usr/sbin/mysqld: unknown variable 'master-host=192.168.140.120'
131022  8:17:57 [ERROR] Aborting

Thing is the replication entries in my.cnf need to be removed/commented out and salve needs to be set up from MySQL console.

So here is my new my.cnf.

[11:34:25] xavi@linode1: ~ $ sudo grep -v “^#” /etc/mysql/my.cnf | grep -v “^$”

[client]
port            = 3306
socket          = /var/run/mysqld/mysqld.sock
[mysqld_safe]
socket          = /var/run/mysqld/mysqld.sock
nice            = 0
[mysqld]
server-id       = 2
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
language        = /usr/share/mysql/english
skip-external-locking
bind-address            = 192.168.137.234
key_buffer              = 16M
max_allowed_packet      = 16M
thread_stack            = 128K
thread_cache_size       = 8
myisam-recover          = BACKUP
query_cache_limit       = 1M
query_cache_size        = 16M
expire_logs_days        = 10
max_binlog_size         = 100M
[mysqldump]
quick
quote-names
max_allowed_packet      = 16M
[mysql]
[isamchk]
key_buffer              = 16M
!includedir /etc/mysql/conf.d/

[11:34:37] xavi@linode1: ~ $

We load the above file or similar depending on your configuration. Now to configure slave we log into the MySQL CLI.


mysql> STOP SLAVE;
mysql> CHANGE MASTER TO MASTER_HOST=’192.168.140.120′, MASTER_USER=’replication-user’, MASTER_PASSWORD=’password’, MASTER_LOG_FILE=’mysql-bin.000747′, MASTER_LOG_POS=75797;
mysql> START SLAVE;
[/text]

That’s it.

DNS records creator

Back around here. Quick post of a small script to create A and PTR dns records.

Input file. First column is the IP and second column is the fqdn. We call this file hosts.txt

10.124.12.34 athletic.abc.com
192.158.21.32 deportivo.abc.com
92.32.43.12 drac1.abc.com

Script below:

#!/bin/bash

while read line; do
        IP=`echo $line | awk '{print $1}'`
        HOST=`echo $line | awk '{print $2}'`
        PTR=`echo "${IP}" | awk -F\. '{print $4"."$3"."$2"."$1".in-addr.arpa."}'`
        echo "${HOST}. IN A ${IP}"
        echo "${PTR} IN PTR ${HOST}."
done < hosts.txt

Execution:

me@server:/tmp$ bash dnsconverter.sh
athletic.abc.com. IN A 10.124.12.34
34.12.124.10.in-addr.arpa. IN PTR athletic.abc.com.
deportivo.abc.com. IN A 192.158.21.32
32.21.158.192.in-addr.arpa. IN PTR deportivo.abc.com.
drac1.abc.com. IN A 92.32.43.12
12.43.32.92.in-addr.arpa. IN PTR drac1.abc.com.
me@server:/tmp$