Thursday, April 29, 2010

Trailing Zeros of a Factorial

    0 /*
1 * Problem: To find the number of trailing zeros in a factorial (n)
2 * URL: http://www.codechef.com/problems/FCTRL/
3 * Input: n
4 * Output: No. of Trailing Zeroes
5 * Formula: E(sigma) limit (i=1..k) = [n/5^i]
6 * k is chosen such that 5^k+1 < n
7 * Execution Time: 1.49 (Alloted 8 Sec)
8 */
9
10 #include <iostream>
11
12 using namespace std;
13
14 int main() {
15 int n, trail_zeros;
16 long factor5, num;
17
18 cin>>n;
19 for (int i=0; i<n; i++) {
20 trail_zeros = 0;
21 factor5 = 5;
22 cin>>num;
23 while (factor5 <= num) {
24 trail_zeros += num / factor5;
25 factor5 *= 5;
26 }
27 cout<<trail_zeros<<endl;
28 trail_zeros = 0;
29 }
30 return 0;
31 }
32


The Program in bc (An arbitrary precision calculator language):

    0 define f(x) {
1 if (x>1) {
2 return (x * f (x-1))
3 }
4 return (1)
5 }
6
7 define z(x) {
8 if (0 == x) return 0;
9 if (0 == x%10) {
10 return 1+z(x/10)
11 }
12 return 0
13 }
14
15 define trial_zero(x) {
16 return z(f(x))
17 }
18

Thursday, March 25, 2010

Programming Quotes

"Programming is art of expressing solutions to problems so that a computer can execute the solutions." - Bjarne Stroustroup

"Clearly, I reject the view that there is one way that is right for everyone and for every problem." - Bjarne Stroustrup

"Your Quote Here." - Bjarne Stroustrup (Templates Chapter - The C++ Programming Language Book).

Wednesday, March 24, 2010

Makefile Explained - Part-1

Sorry for a long delay. I was held up tight in my previous project. Now, I have time to breathe, and enjoy the blue sky.

Let is say I have a file hello.cpp, now how we compile it:
c++ -o hello hello.cpp

if you have installed g++:
g++ -o hello hello.cpp

Generic format:

{compiler name} -o {output file} {source file}

-o - option to specify output filename. If we don't specify {output file}, by default the output will be written to "a.out" file.

"a.out" - assembler output

A Simple Makefile:

hello: hello.cpp
{tabspace}g++ -o hello hello.cpp

Output:
$make hello
g++ -o hello hello.cpp
$

Look at the output, when I give "make hello" command, the make command searches the "hello" target in the makefile and executes the command given for "hello" target.

This makefile make our life easier, we don't need to type "g++ -o hello hello.cpp" everytime, instead we can just give "make hello" which will take care of compiling it.

In my next post, we will see how Make utility can make our life more easier.

Tuesday, February 2, 2010

Pthread Part-1

A Simple Pthread Program:
    0 #include <stdio.h>
1 #include <unistd.h>
2 #include <pthread.h>
3
4 void *fun(void *args) {
5 int i;
6
7 for (i=0; i<10; i++) {
8 printf("hello world\n");
9 sleep(1);
10 }
11 pthread_exit(NULL);
12 }
13
14 int main() {
15 pthread_t pt_id;
16
17 printf("Main: Before Create\n");
18 pthread_create(&pt_id, NULL, fun, NULL);
19 printf("Main: After Create\n");
20 pthread_join(pt_id, NULL);
21 printf("Main: After Join\n");
22 pthread_exit(NULL);
23 return 0;
24 }
25


Output:
  
$./pthread1
Main: Before Create
Main: After Create
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
Main: After Join
$

Thursday, January 7, 2010

Generic Makefile

Here is a Generic Makefile to compile xxx.c++ to xxx executable:
    0 CC = g++
1 CFLAG = -Wall -g -ansi
2
3 %: %.c++
4 $(CC) $(CFLAG) -o $@ $^
5
It will be very useful for all our sample codes.
For example:
hello.c++:
    0 #include <iostream>
1
2 using namespace std;
3
4 int main() {
5 cout<<"hello world"<<endl;
6 return 0;
7 }
$ make hello
g++ -Wall -g -ansi -o hello hello.c++
$ ./hello
hello world
$

Welcome to Programmer's Quest

Why did I create Prog Heaven ? What can you find in this blog ?

I started this blog as part of my New Year Resolution to start a Blog for Start-up Programmers. I'm going to post a variety of Programming Blog-posts which include:

* C Programming
* Unix/Linux System Programming
* Unix/Linux Network Programming
* C++ Programming
* Object Oriented Analysis & Design
* Unified Modeling Language - UML
* Functional Programming (Scheme, LISP, Haskel)
* Logic Programming (Prolog)
* Programming Problem Solving
* Algorithms & Data Structures
* Software Engineering (mostly Construction)
* and Other Programming Related Articles ...

I will also try to answer any question & comments related to programming. Enjoy the ride with me.