Sponsored Content
Top Forums Programming Programming Challenges - A List Post 302120508 by Octal on Wednesday 6th of June 2007 07:20:09 PM
Old 06-06-2007
Quote:
Originally Posted by blowtorch
Challenge: DRAM

Difficulty: Easy to Medium in C, Easy in perl/python

As the problem says, the value of the palindrome may exceed your system's defined integer size in C/C++.
My solution:
Code:
#include <stdio.h>
#include <stdlib.h>

long dram(long);
int palin(long);
long reverse(long);

main(int argc, char *argv[]) {
	long n;
	if (argc < 2) {
		fprintf(stderr, "usage: %s [number]\n", argv[0]);
		exit(1);
	}
	if (argc == 2) {
		n = atoi(argv[1]);
		printf("%ld\n", dram(n));
	} else {
		while (--argc > 0) {
			n = atoi(*++argv);
			printf("%ld\n", dram(n));
		}
	}
}
long dram(long n) {
	long n2;
	while (palin(n) != 1) {
		n2 = reverse(n);
		n += n2;
	}
	return n;
}
int palin(long n) {
	long n2 = reverse(n);

	if (n == n2) {
		return 1;
	}
	return 0;
}
long reverse(long n) {
	long rev = 0;

	while (n > 0) {
		rev *= 10;
		rev += n%10;
		n /= 10;
	}
	return rev;
}

I'll work on the bases challenge later.
 

3 More Discussions You Might Find Interesting

1. AIX

AIX 6.1 IDSLDAP Installation Challenges

Please bare with me, since I am new to AIX and LDAP. I am attempting to install idsldap server on our AIX 6.1 NIM server. I installed the following packages: root@nim(/)# lslpp -l|grep ldap db2_08_01.ldap 8.1.1.80 COMMITTED DB2 LDAP Support idsldap.clt64bit61.rte 6.1.0.17 COMMITTED... (6 Replies)
Discussion started by: ecollins
6 Replies

2. UNIX for Advanced & Expert Users

Challenges in finding and copying the block

Hi, I have a below challenging task where iam unable to find the block and copy the same into a file. I tried my luck,howver iam unable to reach the first and second step..Can anyone help me with a clue or with the commands so that i can give a try. 1. search the <number>9966993366</number>... (2 Replies)
Discussion started by: cskumar
2 Replies

3. Shell Programming and Scripting

Korn shell script - SQL statement challenges

Hi scripting experts. I have some coding challenges that I'm hoping you can help me out. I have one file#1 that contains the following sql statement that spans over multiple lines: sql Select /*+ use_has(a,b) */ * from customer a, customer_address b where a.id = b.id... (1 Reply)
Discussion started by: pchang
1 Replies
CACOS(3)						     Linux Programmer's Manual							  CACOS(3)

NAME
cacos, cacosf, cacosl - complex arc cosine SYNOPSIS
#include <complex.h> double complex cacos(double complex z); float complex cacosf(float complex z); long double complex cacosl(long double complex z); Link with -lm. DESCRIPTION
The cacos() function calculates the complex arc cosine of z. If y = cacos(z), then z = ccos(y). The real part of y is chosen in the interval [0,pi]. One has: cacos(z) = -i * clog(z + i * csqrt(1 - z * z)) VERSIONS
These functions first appeared in glibc in version 2.1. CONFORMING TO
C99. EXAMPLE
/* Link with "-lm" */ #include <complex.h> #include <stdlib.h> #include <unistd.h> #include <stdio.h> int main(int argc, char *argv[]) { double complex z, c, f; double complex i = I; if (argc != 3) { fprintf(stderr, "Usage: %s <real> <imag> ", argv[0]); exit(EXIT_FAILURE); } z = atof(argv[1]) + atof(argv[2]) * I; c = cacos(z); printf("cacos() = %6.3f %6.3f*i ", creal(c), cimag(c)); f = -i * clog(z + i * csqrt(1 - z * z)); printf("formula = %6.3f %6.3f*i ", creal(f), cimag(f)); exit(EXIT_SUCCESS); } SEE ALSO
ccos(3), clog(3), complex(7) COLOPHON
This page is part of release 3.44 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. 2011-09-15 CACOS(3)
All times are GMT -4. The time now is 04:29 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy