Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Sunday, 12 August 2012

Calculate a PRIME number LIST using C language


This C program calculates a list with first prime numbers.

Build the program


C is a compiled language, so we will need to install a C compiler: gcc
$ sudo aptitude install gcc

We copy this text in a file, e.g: prime_calculator.c

Next we compile the file:
$ gcc -o prime_calculator -lm prime_calculator.c -Wall

or if we want further optimization:
$ gcc -o prime_calculator -lm prime_calculator.c -Wall -O3

-lm option links the math library so we can calculate square roots.

Execute the program


After compilation, an executable appears: prime_calculator.
$ chmod a+x prime_calculator # we grant it execution permission.

If we want to show first ten prime numbers:
$ ./prime_calculator 10
PRIME LIST:
2 3 5 7 11 13 17 19 23 29

If we pass more than two arguments, it calculates the prime list but does not show anything. Just to measure its execution time:
$ time ./prime_calculator 10 *
real 0m0.008s
user 0m0.004s
sys 0m0.000s

Saturday, 21 April 2012

How to set EMACS C STYLE to hack KAMAILIO code

Kamailio (former OpenSER) is a SIP proxy server, call router, and user agent registration server used in Voice over Internet Protocol and instant messaging applications. Kamailio is free software (GPL licensed).

In this post we are going to learn how to configure emacs to follow kamailio coding style guidelines.


KAMAILIO CODING STYLE GUIDELINES


From Kamailio documentation: http://sip-router.org/wiki/coding_style

As a resume:
  • Use tabs for indentation instead of spaces.
  • Set tab stops to 4 spaces.
  • Wrap lines that are longer than 78 characters (80 characters - two characters for window decorations).
  • Avoid C++ style comments (//)
  • Do not declare variables inside blocks.
  • Use the following style for function declarations
  • int func(int x)
    {
        /* body */
    }
    


EMACS C KAMAILIO CODING STYLE


We are going to create an emacs C coding style in order to comply with most of those rules.

Wednesday, 20 July 2011

Calculate Prime Numbers using an ARDUINO Board

Following example uses an Arduino board to calculate a prime number list, starting from one.
It shows the result using serial port communication.

BUILDING AND RUNNING THE CODE


You should copy this code into your Arduino IDE window, compile it (C-R), upload the code into the board (C-U) and to see the result open the serial monitor (C-Shift-M).

If everything goes fine your serial monitor will show a prime on each line every time it is calculated. Maximum prime able to be calculated depends on long type maximum value.

long number = 3;

void setup()
{
  Serial.begin(9600);
  Serial.println("#");
  Serial.println("1");
  Serial.println("2");
}

void loop()
{
  int is_prime = 1;
  long i = 3;
  long top = sqrt(number);
  while (i<top)
  {
    if (number%i == 0)
    {
      is_prime = 0;
      break;
    }
    i++;
  }
  if (is_prime)
  {
    Serial.println(number,DEC);
  }
  number += 2;
}

Within setup function, it shows 1 and 2 primes. It then starts calculating in the main loop using number 3.

Primality test here consists on performing divisions and checking the remainder. It is not needed to test with every divisor, when square root is reached we know it is a prime.

When a prime number is found it is sent over serial line.

As we start calculations with number three, we can skip all even numbers.


YOU MAY ALSO BE INTERESTED IN:


Install and run a program in Arduino using 64 bits Ubuntu (lucid) distro

Sunday, 6 July 2008

Hello World program examples

Hello World is the first program every user usually codes when he learns a new programming language.

Its goal is learning the basics to edit, compile and execute a program which sends a "hello world" string to standard output.


We have used an Ubuntu Hardy distro based system. Most commands will serve to other distros as well, like Debian.


BASH Shell Scripting Hello World


bash command line interpreter is usually already installed in your system.

if it is not installed we type: $sudo aptitude install bash


Now we create hello.sh script using whatever text editor we prefer.
e.g: $ gedit hello.sh


Copy this lines into your hello.sh script
#!/bin/bash
echo "hello world"


Bash shell scripting is an interpreted language so we have not to compile anything. We directly execute the shell script file:

We give the shell script execution permissions:
$ chmod a+x hello.sh

and launch the program
$ ./hello.sh


PYTHON Scripting Hello World


If you do not have python installed in your system, exec:

$ sudo aptitude install python