Hacking with an algorithm to find prime factor of a number

$./app 
Sign detection [1] 
Power of two [2] 
Counting No. of bits set [3] 
Set or clear bits without branching[4] 
Find maximum value[5] 
Finding least common multiple[6] 
Finding the greatest commom divisor[7] 
Finding if a number is an Armstrong number or not[8] 
Finding if a given number is prime number[9]  
Finding the number of twin prime numbers[10] 
Finding if a number is palindrome or not[11] 
Finding prime factor of a number[12] 
12
Enter a number: 36

 2 is a prime factor 

 3 is a prime factor 
$./app 
Sign detection [1] 
Power of two [2] 
Counting No. of bits set [3] 
Set or clear bits without branching[4] 
Find maximum value[5] 
Finding least common multiple[6] 
Finding the greatest commom divisor[7] 
Finding if a number is an Armstrong number or not[8] 
Finding if a given number is prime number[9]  
Finding the number of twin prime numbers[10] 
Finding if a number is palindrome or not[11] 
Finding prime factor of a number[12] 
12
Enter a number: 76

 2 is a prime factor 

 19 is a prime factor 
$bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
76/19
4


How to write an algorithm to find if a number is palindrome or not

#include <stdio.h>

palindrome()
{

int n, reverse = 0, temp;
printf("Enter a number to check if it is a palindrome or not: ");
scanf("%d",&n);
temp = n;

while ( temp != 0 )
{
reverse = reverse * 10;
reverse = reverse + temp % 10;
temp = temp/10;
}

if ( n == reverse )
printf("%d is a palindrome number \n",n);
else
printf("%d is not a palindrome number \n",n);
}

Tinkering with an Armstrong number finding program function

$./app 
Sign detection [1] 
Power of two [2] 
Counting No. of bits set [3] 
Set or clear bits without branching[4] 
Find maximum value[5] 
Finding least common multiple[6] 
Finding the greatest commom divisor[7] 
Finding if a number is an Armstrong number or not[8] 
8
Enter a positive integer: 153
153 is an Armstrong number 
$./app 
Sign detection [1] 
Power of two [2] 
Counting No. of bits set [3] 
Set or clear bits without branching[4] 
Find maximum value[5] 
Finding least common multiple[6] 
Finding the greatest commom divisor[7] 
Finding if a number is an Armstrong number or not[8] 
8
Enter a positive integer: 154
154 is not an Armstrong number 
$./app 
Sign detection [1] 
Power of two [2] 
Counting No. of bits set [3] 
Set or clear bits without branching[4] 
Find maximum value[5] 
Finding least common multiple[6] 
Finding the greatest commom divisor[7] 
Finding if a number is an Armstrong number or not[8] 
8
Enter a positive integer: 1
1 is an Armstrong number 
$

Hacking with the program to find the greatest common factor

$./app 
Sign detection [1] 
Power of two [2] 
Counting No. of bits set [3] 
Set or clear bits without branching[4] 
Find maximum value[5] 
Finding least common multiple[6] 
Finding the greatest commom divisor[7] 
7
Enter the two values for finding GCD: 12 6
GCD is: 6
$./app 
Sign detection [1] 
Power of two [2] 
Counting No. of bits set [3] 
Set or clear bits without branching[4] 
Find maximum value[5] 
Finding least common multiple[6] 
Finding the greatest commom divisor[7] 
7
Enter the two values for finding GCD: 5 12
GCD is: 1
$./app 
Sign detection [1] 
Power of two [2] 
Counting No. of bits set [3] 
Set or clear bits without branching[4] 
Find maximum value[5] 
Finding least common multiple[6] 
Finding the greatest commom divisor[7] 
7
Enter the two values for finding GCD: 20 25
GCD is: 5
$./app 
Sign detection [1] 
Power of two [2] 
Counting No. of bits set [3] 
Set or clear bits without branching[4] 
Find maximum value[5] 
Finding least common multiple[6] 
Finding the greatest commom divisor[7] 
7
Enter the two values for finding GCD: 12 8
GCD is: 4
$./app 
Sign detection [1] 
Power of two [2] 
Counting No. of bits set [3] 
Set or clear bits without branching[4] 
Find maximum value[5] 
Finding least common multiple[6] 
Finding the greatest commom divisor[7] 
7
Enter the two values for finding GCD: 32 235
GCD is: 1
$

How to write a program to connect to a PostgreSQL server ? (program may not be perfect )

#include <stdio.h>
#include <stdlib.h>
#include <postgresql/libpq-fe.h>

int main(int argc, char **argv)
{
const char *conninfo;
const char *serverversion;
PGconn *conn;
const char *paramtext = "server_version";
conninfo = "hostaddr = 127.0.0.1 dbname = test user = earl password = bigshot";

conn = PQconnectdb(conninfo);
if ( PQstatus(conn) != CONNECTION_OK )
{

printf("Unable to establish connection: %s",PQerrorMessage(conn));
return 1;
}
else
{
printf("Connection established!\n");
serverversion = PQparameterStatus(conn,paramtext);
printf("Server Version: %s\n",serverversion);
}
PQfinish(conn);
return 0;
}

Tinkering with the fping command

$fping   192.168.0.1
192.168.0.1 is alive
$fping   192.168.0.2
ICMP Host Unreachable from 192.168.0.103 for ICMP Echo sent to 192.168.0.2
ICMP Host Unreachable from 192.168.0.103 for ICMP Echo sent to 192.168.0.2
ICMP Host Unreachable from 192.168.0.103 for ICMP Echo sent to 192.168.0.2
ICMP Host Unreachable from 192.168.0.103 for ICMP Echo sent to 192.168.0.2
192.168.0.2 is unreachable
$fping   192.168.0.3
ICMP Host Unreachable from 192.168.0.103 for ICMP Echo sent to 192.168.0.3
ICMP Host Unreachable from 192.168.0.103 for ICMP Echo sent to 192.168.0.3
ICMP Host Unreachable from 192.168.0.103 for ICMP Echo sent to 192.168.0.3
ICMP Host Unreachable from 192.168.0.103 for ICMP Echo sent to 192.168.0.3
192.168.0.3 is unreachable
$fping  -c1  192.168.0.1
192.168.0.1 : [0], 84 bytes, 0.29 ms (0.29 avg, 0% loss)

192.168.0.1 : xmt/rcv/%loss = 1/1/0%, min/avg/max = 0.29/0.29/0.29
$fping  -c1 -s 192.168.0.1
192.168.0.1 : [0], 84 bytes, 0.27 ms (0.27 avg, 0% loss)

192.168.0.1 : xmt/rcv/%loss = 1/1/0%, min/avg/max = 0.27/0.27/0.27

       1 targets
       1 alive
       0 unreachable
       0 unknown addresses

       0 timeouts (waiting for response)
       1 ICMP Echos sent
       1 ICMP Echo Replies received
       0 other ICMP received

 0.27 ms (min round trip time)
 0.27 ms (avg round trip time)
 0.27 ms (max round trip time)
        0.000 sec (elapsed real time)

$

Hacking with httping for simple speed study

ABOUT httping

httping - measure the latency and throughput of a webserver

A TYPICAL SHELL SESSION
[bash]
$httping www.beautifulwork.org
PING www.beautifulwork.org:80 (www.beautifulwork.org):
connected to 80.79.116.205:80 (490 bytes), seq=0 time=809.49 ms
connected to 80.79.116.205:80 (490 bytes), seq=1 time=807.21 ms
connected to 80.79.116.205:80 (490 bytes), seq=2 time=788.23 ms
connected to 80.79.116.205:80 (490 bytes), seq=3 time=796.85 ms
^CGot signal 2
— www.beautifulwork.org ping statistics —
5 connects, 4 ok, 0.00% failed, time 7256ms
round-trip min/avg/max = 788.2/800.4/809.5 ms
$httping -c4 -g www.beautifulwork.org
PING www.beautifulwork.org:80 (www.beautifulwork.org):
connected to 80.79.116.205:80 (490 bytes), seq=0 time=786.45 ms
connected to 80.79.116.205:80 (490 bytes), seq=1 time=798.73 ms
connected to 80.79.116.205:80 (490 bytes), seq=2 time=806.86 ms
connected to 80.79.116.205:80 (490 bytes), seq=3 time=883.28 ms
— www.beautifulwork.org ping statistics —
4 connects, 4 ok, 0.00% failed, time 6276ms
round-trip min/avg/max = 786.4/818.8/883.3 ms
$httping -c4 -Gg www.beautifulwork.org
PING www.beautifulwork.org:80 (www.beautifulwork.org):
connected to 80.79.116.205:80 (1434 bytes), seq=0 time=819.49 ms
connected to 80.79.116.205:80 (1434 bytes), seq=1 time=799.30 ms
connected to 80.79.116.205:80 (1434 bytes), seq=2 time=830.68 ms
connected to 80.79.116.205:80 (1434 bytes), seq=3 time=806.28 ms
— www.beautifulwork.org ping statistics —
4 connects, 4 ok, 0.00% failed, time 6256ms
round-trip min/avg/max = 799.3/813.9/830.7 ms
$

$httping -c4 -Gg www.worldcuplivematches.net
PING www.worldcuplivematches.net:80 (www.worldcuplivematches.net):
connected to 119.18.56.76:80 (581 bytes), seq=0 time=6641.52 ms
connect time out
connected to 119.18.56.76:80 (581 bytes), seq=2 time=6047.30 ms
connect time out
— www.worldcuplivematches.net ping statistics —
4 connects, 2 ok, 50.00% failed, time 75717ms
round-trip min/avg/max = 6047.3/6344.4/6641.5 ms
$

$httping -c4 -Gg www.worldcuplivematches.net
PING www.worldcuplivematches.net:80 (www.worldcuplivematches.net):
connected to 119.18.56.76:80 (581 bytes), seq=0 time=628.55 ms
connected to 119.18.56.76:80 (581 bytes), seq=1 time=737.22 ms
connected to 119.18.56.76:80 (581 bytes), seq=2 time=629.63 ms
connected to 119.18.56.76:80 (581 bytes), seq=3 time=648.22 ms
— www.worldcuplivematches.net ping statistics —
4 connects, 4 ok, 0.00% failed, time 5644ms
round-trip min/avg/max = 628.6/660.9/737.2 ms
$

[/bash]