/*
 ====================================================================
 Name		: palindrome.c
 Date		: 2008-06-27
 Author		: Haui, haui45@web.de 
 Version	: 0.1
 Copyright	: GPL
 Description	: Find all numbers from 1 to 999999, which are
 		  palindromic in base 2 and base 16
 ====================================================================
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static char* dectobin(long a, char *binary){
	int counter = 0;
	long tmp = a;
	int i = 0;
	short test = 0;
	for (i=31; i>=0; i--){
		tmp = a >> i;
		if (!(tmp &1) && test) binary[counter++] = '0';
		if(tmp & 1) {
			binary[counter++] = '1';
			test = 1;
		}
	}
	binary[++counter] = '\0';
	return binary;
}

static char* dectohex(long a, char *hex){
	int counter = 0;
	long tmp = a;
	int i = 0;
	short test = 0;

	for (i=7; i>=0; i--){
		tmp = a >> (i*4);
		if (!(tmp & 0xF) && test) hex[counter++] = '0';
		if ((tmp & 0xF) > 0 && (tmp & 0xF) <= 0x9 ){ 
			hex[counter++] = ((tmp & 0xF) + 0x30);
			test = 1;
		}
		if ((tmp & 0xF) >= 0xA && (tmp & 0xF) <= 0xF ){
		       	hex[counter++] = ((tmp & 0xF) + 0x37);
			test = 1;
		}
	}
	hex[++counter] = '\0';
	return hex;
}

/*returns 0 if input is a palindrome*/
int isPalindrome(char *input){
	int length = strlen(input);
	int i = 0;
	int ret = 0;
	for(i=0; i<length; i++){
		if (input[i] != input[length-1-i]){
			ret = 1;
			break;
		}
	}
	return ret;
}



int main(int argc, char **argv){
	char bin[33] = "";
	char hex[9] = "";
	long i = 0;
	for(i=1; i<1000000; i+=2){
		if (!isPalindrome(dectohex(i, hex)) && !isPalindrome(dectobin(i, bin)) ) 
			printf("%ld\n", i);
	}
	return 0;
}
