C#: Wrong answer when finding "cool" numbers.

Posted by user300484 on Stack Overflow See other posts from Stack Overflow or by user300484
Published on 2010-04-03T02:15:39Z Indexed on 2010/04/03 2:23 UTC
Read the original article Hit count: 303

Filed under:

Hello you all! In my application, a "cool" number is a number that is both a square and a cube, like for example: 64 = 8^2 and 64 = 4^3. My application is supposed to find the number of "cool numbers" between a range given by the user. I wrote my code and the application runs fine, but it is giving me the wrong answer. Can you help me here please? for example:

IMPUT

1 100

OUTPUT

1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            double a = Convert.ToDouble(Console.ReadLine()); // first number in the range
            double b = Convert.ToDouble(Console.ReadLine()); // second number in the range
            long x = 0;
            for (double i = a; i <= b; i++)
            {   

                   double cube = 1.0 / 3.0;
                   double cuad = 1.0 / 2.0;
                   double crt = Math.Pow(i, cube); // cube root 
                   double sqrt = Math.Pow(i, cuad); // square root


                if ((crt * 10) % 10 == 0 || (sqrt * 10) % 10 == 0) // condition to determine if it is a cool number.
                    x++;

            }
            Console.WriteLine(x);
            Console.ReadLine();
        }
    }
}

© Stack Overflow or respective owner

Related posts about c#