### Archive

Archive for August, 2010

## Find factorial of a given number

Here I’m going to illustrate the method for finding the factorial of a given number(<100) programmatically, which is different from that of finding factorial using long, int, double etc., In this I’m going to use array to store each digit of solution.

Method:

The method I used in my code is similar to that of natural multiplication.

Let us assume that we have two numbers 43 and 25 to be multiplied. Here I’m considering 43 is kept in an array as digit ‘3’ 0th index and digit ‘4’ 1st index. Now I’m multiplying 25 with 0th index digit and adding temp(which is initially zero), we get the sum. Now find total sum%10 which gives the value to be stored at that index. i.e a[0]=5, temp=7. In the next iteration a[1]=7 temp=10

those two ‘1’ and ‘0’ digits are appended at the end of the array.

Resulting a[0]=5 a[1]=7 a[2]=0 a[3]=1

1075

Let me put it in an algorithmic way.

N //given input

temp=0 //initially

a[200] // array where the given input N is stored

m //stores number of digits in a given input

Repeat until n becomes 1

do

n–

a[index]=(a[index]*n+temp)%10

temp=(a[index]*n+temp)/10

done

while temp is not zero

do //the resultant is more than m digit number so increment m

m++

a[m]=temp%10

temp=temp/10

done

Here is the working code in online editor where you can give input and also check for output.

input should be given in the following way.

3                 — Which means number of inputs to follow

12

23

43

Categories: Programming, Technology

## Divide and Conquer method Illustrated

Divide and Conquer is one of the famous algorithmic techniques. It works with the philosophy that divide the whole problem into smaller managable chunks of subproblems and work out these small subproblems there by combining the intermediate partial solutions.

There are many famous examples which use the Divide and Conqure strategy, for example Binary search, Merge sort, Insertion sort, Quick sort etc.,

Let us take a small example of finding minimum and maximum elements in a given array. If we adapt a normal methodology it takes atleast 2*(n-1) comparisions. We can decrease the number of comparisions using this technique which results in [ Ceil(3*n/2)-2 ] comparisions.

The below is the code for finding MIN and MAX of the given array which was compiled and tested using GNU g++compiler.

```//Author: Vasanth Raja Chittampally
#include <iostream>

using namespace std;

void findMin(int [],int, int,int &amp;, int &amp;);

int main()

{

int n,min,max;

cout<<"Enter n:";

cin>>n;

int a[n];

for(int i=0;i<n;i++)

{

cout<<"Enter a["<<i<<"]:";

cin>>a[i];

}

findMin(a,0,n-1,min,max);

cout<<"Minimun of the given array is: "<<min<<;endl;

cout<<"Maximum of the gvien arrays is: "<<max<<endl;

return 0;

}

void findMin(int a[], int left, int right, int &amp;min, int &amp;max)

{

if(left==right)

{

min=a[left];

max=a[right];

}

else

{

int mid=(left+right)/2;

findMin(a,left,mid,min,max);

int left_min=min;

int left_max=max;

findMin(a,mid+1,right,min,max);

int right_min=min;

int right_max=max;

if(left_min&lt;right_min)

min=left_min;

else

min=right_min;

if(left_max&gt;right_max)

max=left_max;

else

max=right_max;

}

}
```

In this example it divides whole array into two equal parts recursively and tries to compare partial solutions to find the minimum and maximum elements.

T(n)=2T(n/2) + 2 —–>(2 comparisions are needed one for minimum and the other of maximum)

.

.

.

.

number of comparisions = [ Ceil(3*n/2)-2 ]

The above number of comparisions can be verified by substituting in T(n)

T(n)=2*[3*n/(2*2) – 2]+2

T(n)=3n/2-2 (hence verfied)

## What is the smallest number divisible by each of the numbers 1 to 21?

public class Projec {

public static void main(String[] args) {

out:for (int i = 2520; ; i++) {
for (int j = 2; j < 21; j++) {
if(i%j==0) ;
else
continue out;
}
System.out.println(“”+i);
break out;
}
}

}

But the true way of finding the number is as follows

To find a number that is divided by 1 to 6

1*2*3*2*5=60

FOR 1 to 21

2 * 3 * 2 * 5 * 7 * 2 * 3 * 11 * 13 * 2 * 17 * 19 = 232792560

Categories: Programming

## Wroking with iptables

Iptables: Is an application that allows network administrator to create rules on Kernel level firewall. It also one kind of packet filtering mechanism. The iptables were purely written in the C Language. Iptables are used with IP V4.

Iptables executable binary file can be found in the /sbin/iptables(type “which iptables” in the terminal see the result). Iptables requires root access to work with. User must be logged in as root to view, append, delete or change existing rules.

The Linux kernel contains the built-in ability to filter packets, allowing some of them into the system while stopping others. Most of the Linux Kernels come with the following tables

1) nat: This is the default table for handling network packets.

2) filter: This table used to alter packets that create a new connection.

3) mangle: This table is used for specific types of packet alteration.

Each of these tables in turn have a group of built-in chains, they are

1. INPUT — This chain applies to packets received via a network interface.
2. OUTPUT — This chain applies to packets sent out via the same network interface which received the packets.
3. FORWARD — This chain applies to packets received on one network interface and sent out on another.

Every chain has a default policy to ACCEPT, DROP, REJECT, or QUEUE the packet to be passed to user-space. If none of the rules in the chain apply to the packet, then the packet is dealt with in accordance with the default policy.

The iptables command allows you to configure these rule lists, as well as set up new tables to be used for your particular situation

Categories: Technology

## SQL Injection Attacks

Our dependance with the web has rapidly increased, now internet has took the world by storm. Most of us use internet for e-banking, online marketing, paying bills and making reservations. We have to ask ourselves how much safe and reliable the webapplications are?. Studies have found that many webapplications’ security is compromised by the sophisticated attacks One of such kinds is “The SQL Injection”. In this attack attacker injects his code in to the web application thereby compromising the security, he can leak very confidential information such as Credit card numbers and even can able to destroy all the data in the database too.

The SQL Injection attack is possible because the given user input is not validated before it is fed to the database SQL engine. A part of the user input is written by the attacker such that the data is totally aired to the attacker. If we go in detail, the main cause of concern of this attack is owing to having been totally stuck up to the management of data using databases(Databases are the best way of organising and storing the data).

One of the counter measure for this kind of attack is that using techniques which will prevent Invalid and destructive User Inputs. Here is an example how is this done.

Let there is are UserName: and Password: fiels in a webspage. When user enters username and password the developer would have written the SQL query to evealuate it Like

{

//

}

After this is over assume that there is another textfield to search within the website. At that time the attacker gives the query in this form.

;’ Malicious Qurery ‘;

Select plan from Web where search=”;’Malicious Qurey’;
The first ;(semicolon marks the end of the internal query as shown in the above statement) and the second query executes happily harming the whole database totally breaching the security.

The Malicious Query could be “drop table User” ( Or anything)

One kind of couter measures(or mitigation technique) we have to take against SQL Injection is using PreparedStatements in java while coding for the webapplications. In PreparedStatements it takes whole user input as only text “String” No matter what it is.

Categories: Technology

## Way to Implement Shared Memory Concept in Windows

It is a bit tricky to implement shared memory concept in windows because all the internal memory management(Kernel Level) details are not open up by Microsoft, In contrast, it is very straight forward in Linux systems.

Here is the link to understand the FileMapping concept.

## Make Googling more productive and Faster

Do you use google for atleast once in a day? Do you want to learn how to make your search much easier? Have you ever tried to learn how to make googling very fun?

Here are the tips and techniques to make your googling experience more easier and to get to the desired results much faster.

The Genesis of Search Engine

After Tim Berners Lee’s invention of WWW, The usage of internet took the world by storm. The ever increasing information in the web made the search engine inevitable which constituted the concrete need for a search engine. The very first concrete web search engine came into existence in 1990 named “Archie”. Later on many came into existence but not as many have got so much popularity as only some, including Google, Yahoo, Bing.

The recent study, conducted on two similar teams one who uses google or other search engines and the other never any search engine, came to an interesting conclusion that the first group (who use search engine frequently) members’ brain functioning has improved significantly and the other group members’ brain has very less improvement.

Here are some of the new ways of making searching easier.

1) Find all the images of given website using an attribute called “site”

type the following in the search bar “site:sitename

If you want to find a particular image inside that website add that to the above sytax

ex: “site:chandamama.com Birbal” (Without double quotes) this shows all the images with Birbal as image name in chandamama.com website.

2) Find all the websites which have particular key word as a part of its url using “inurl” attribute

ex: “inurl:vasanthexperiments” this gives all the websites which have “vasanthexperiments” as a part of their url.

ex: type “seconds in year” and see the first result.

### 1 year = 31 556 926 seconds

Similarly

type and see the following also

20/5

4*5-7+45=”

4) Find meaning for a word using the following syntax

define:lookingUpWord

5) i) Search for not only specified word but also for its synonyms of a given word using the following syntax

5) ~lookingUpWord

ex: ~reincarnation

ii) Exclude some words in the synonyms using the following sytax

6) ~lookingUpWord -excludingWord[s]

ex: ~content -happy -merry

In the above example google searches for not only the given word but also its synonyms of word “content” with out including results having words happy and merry.

6) Search for a phrase exactly using the [“”]

i) ex: if you want to search for a phrase exactly as it was specified surround that word with double quotes for example “Vasanth Raja” it searches for the exact match of “Vasanth Raja” and it ignores all the other results consisting of words like “Vasanth ch. Raja”

7) Search with in only class of domains using the above specified attribute called “site

for example if you want to search for “economic deficit” in all the government sites you could give “economic deficit site: .gov

8. Fill in the blanks

if you want to fill some of the words you can do by using the wild card character “*” which has different interpretations based on different context.

Ex: “Manmohan voted * on * bill” this(without the double quotes”) gives all the results containing some formation of all the above specified key words.

9) Google has the default behaviour of considering the searching words with “AND” operator between them. For example “San Francisco Giants 2004 2005”(Without double quotes”) the search results give all the pages which include both 2004 and 2005. If you want to get results for either 2004 or 2005 use an operator called “OR” (Upper case letters(mandatory)).

Ex: “San Francisco Giants 2004 OR 2005” (Without double quotes) gives all the results including San Francisco Giants 2004 or San Francisco Giants 2005.

10) At last make your search much faster by combining all the above techniques and forming much complex query.

All search engine experts and geeks use all the above operators in combination with search words to find the pages they need much faster.

If you like this post or have any suggestions please comment.