Monthly Archives: December 2011

Programming Challenges and Contests Online

I am noticing an interesting trend lately: there are a bunch of website emerging with the goal of connecting programmers and tech companies by using programming challenges and contests. Some that you might wanna check out: Gild.com CodeEval InterviewStreet Code Chef Top Coder is probably the pioneer in this area, by those upcoming sites seem […]

C Programming Puzzle #3

The code of the previous puzzle was the following: #include <stdio.h> void foobar(char *str1, char *str2){   while (*((str1++)+6) = *((str2++)+8)); } int main(){   char str1[] = "Hello World";   char str2[] = "Foo Bar Bar";   foobar(str1,str2);   printf("%s %sn",str1, str2);   return 0; } The answer is that it prints “Hello Bar Foo Bar Bar”. Function foobar will […]

How To Concatenate Two Integers in C

This might not be the most efficient way, but it’s certainly one of the easiest. You simply need to convert both integers into strings using sprintf, concatenate those using strcat, and then reverse the combined string to an integer using the atoi function. int concat(int x, int y){     char str1[20];     char str2[20];     sprintf(str1,"%d",x);     sprintf(str2,"%d",y);     strcat(str1,str2); […]

C Programming Puzzle #2

The code of the first puzzle was: #include <stdio.h> int main(){     if("Hello World" == "Hello World"){        printf("Yes ","No ");     }     printf(10+"Hello World"-4); return 0; } The answer is that it prints “Yes World”. First of all the statement inside the IF will be executed because we compare the same pointer, which returns 1 (although […]

Solution to Project Euler 8

In Problem 8 you need to find the greatest product of five consecutive digits of a 1000-digit number that is given. To find the solution I simply read the number into an array and then traversed it comparing the product of five digits at a time. #include <stdio.h> int main(){   int vet[1000];   int z,max,c,j,i=0;   while(i<1000){ […]

Basic Modular Arithmetic

If you have been programming for a while you probably already used the mod operator a lot. Same here. What I hadn’t done was to explore the world of modular arithmetic, and it turns out it’s quite interesting and useful (e.g., cryptography). First of all let’s talk about the mod, or modulus, operator itself. The […]

Fast Exponentiation Algorithms

Exponentiation is a very common part of mathematics, and it’s involved in many programming puzzles. If you don’t have a function already implemented for you, a simple algorithm to compute a^b (a to the power of b) would be: int expo(int a, int b){     int result = 1;     while (b>0){         result *= a;         b–;     } […]

Solution to Project Euler 7

Here’s the description of the Problem 7: By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10 001st prime number? In order to find the solution I wrote a simple function to test whether a number n is […]