Question Enter number, nothing happens?

Xhalite

New member
Joined
Oct 25, 2013
Messages
1
Location
Austin Tx
Programming Experience
1-3
Hello, I'm relatively new to C#. I'm working on my computer science major and the only language I can safely say that I know fairly well is python (Though I have done stuff with C++ and Java). I chose C# because I like Windows and it looked like a good hybrid between Java and C++. Anyway, I had just finished a homework in python code, and tried implementing it in C# with my limited knowledge of this language and when I enter a value for the variable number, nothing happens. The console just stops working or something after I've entered the number at runtime. If anyone could help me understand what I did wrong I'd appreciate it very much. My code is in the attached file(formatting was weird in code tags):


Oh yeah, I'm new to the forums so... hi. I'll post an introduction later, this was just on my mind when I registered.
 

Attachments

wim sturkenboom

Well-known member
Joined
Aug 6, 2014
Messages
85
Location
Roodepoort, South Africa
Programming Experience
10+
Your program is hanging in this method.

C#:
public static int primeFactorPower(int n, int p)
{
    int count = 1;

    while (n % (Math.Pow(p, count)) == 0)
    {
        count += 1;
    }

    return count - 1;
}
If n==1 and p==1, the result of the modulo is always 0 (as 1^number always equals 1) and you get stuck in an endless loop.

On a side note, read up on comparison of doubles and floats; those types are not 100% accurate so the result of the modulo might be 0.00000000000000000000000001; close enough for us humans but not for computers ;)

// OOPS: as a newbie, I'm reviving old threads :(
 
Last edited:
Top Bottom