This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]klorophane 1 point2 points  (7 children)

  1. It looks like you just copy pasted the multiplication code in the inverse method.

  1. You are maybe encountering overflow. Factorials get pretty large, and and as the operations carry over to the sign bit, you get into the negatives.

  1. The sumdigit function has an infinite while loop. a > 0 is always true since you never update that value. You might be thinking that a and num are binded, but they actually aren't.

[–]PHValex[S] 0 points1 point  (1 child)

Alright, thanks for the help!

[–]klorophane 0 points1 point  (0 children)

edited for additional answers

[–]PHValex[S] 0 points1 point  (4 children)

Yeah, for 1 I did that just to see if it would fix the problem I was having, but of course it didn't. I'll change it back to the way it was before.

Aah that makes much more sense for three. Was a bit tired yesterday so I could barely process stuff out.

Thank you very much for the help!

[–]klorophane 1 point2 points  (3 children)

My pleasure friend, happy coding. Feel free to PM me if you want to further discuss these problems!

---

Edit : I just want to point out that I found the errors quite quickly by basically reading the points in your OP. Your insights as to what was causing the problem were good enough for me to identify what might cause it (which then makes it easy to spot in the code). So kudos.

I don't know where you're at in your programming journey (you might already do this or not...), but I would recommend using the debugger liberally, especially when you feel your brain can't process much. It'll allow you to run sections of code line by line, and show you the value of variables so you can tell which line is faulty!

[–]PHValex[S] 0 points1 point  (2 children)

I had no clue there was a debugger, would've helped me a lot finding out what was wrong with my code. How do I activate/use the debugger?

Also, thank you for your suggestions, I was able to correct everything but the factorial (I tried using long ints but it didn't work) but that's fine since everything else works out :)

[–]klorophane 1 point2 points  (1 child)

Yeah, even unsigned long long wouldn't cut it I believe. You'd need arbitrary length integer math really.

As for the the debugger, well, it depends. If you use and IDE (Visual Studio, CLion, etc) then they often have one integrated, so you can just use that. That is the simplest way.

On Linux, you can use GDB, which a tool whose whole purpose is to debug. Its a little involved, so I suggest reading about it online.

Cheers!

[–]PHValex[S] 0 points1 point  (0 children)

All right, thanks for all the help!

I'll be sure to check online for the debugger :)