Hello,

I'm trying to solve an ACM problem "Light, more light". The problem is actually solved, but I can get it accepted. So browsing Internet I found a C code, which is accepted:

#include<stdio.h>
#include<math.h>

int main(void) {
	long long int n;
	while(scanf("%lld", &n)==1) {
		if(n==0)
			break;
		else if( (long long int)sqrt((double)n) * (long long int)sqrt((double)n) == n)
			printf("yes\n");
		else
			printf("no\n");
	}
	return 0;
}

However, it doesnt' work in Eclipse's console (MinGW GCC toolchain). Why is that?

But the main problem is not that. The task states, that the number won't exceed 2^32 - 1, so in my own solution I used unsigned int:

#include <stdio.h>
#include <math.h>

int main() 
{		
	unsigned int n;	
	while (scanf("%d", &n) == 1)
	{			
		if (n == 0) 
			break;
		else 
		{
			unsigned int root = (unsigned int)sqrt((double)n); 
			if (root*root != n) 
			{	
				printf("no\n");
			}
			else 
			{
				printf("yes\n");
			}			
		}
		
	}
	
	return 0;
}

... which is almost the same, works fine in Eclipse's console, but doesn't get accepted on ACM neither as ANSI C, nor as C++. Can anyone tell me where is the mistake?

the problem with the code using unsigned it is that nowhere in the c++ standard does it say std::numeric_limits<unsigned int>::max() >= 2^32 - 1 or for that matter, std::numeric_limits<unsigned int>::radix == 2 or std::numeric_limits<unsigned int>::digits >= 32 .
so the code may fail for some input values on some implementations.

the code that was accepted is neither ISO C++ nor ANSI C. neither talks about a type long long.
the C99 standard introduced the long long int type ( to be an integral type with at least 64 bits) and it is a long-supported extension by almost all compilers. (C++0x adds this type as well as unsigned long long as fundamental types). by now, long long int (abbreviated as long long) has become a de facto standard (it is de jure only in C99). which is probably why it was accepted.

This article has been dead for over six months. Start a new discussion instead.