+ 1

C# Algorithm Pi.Works till 32?And then Exception.The problem was deciding!!!))

C# Works till 32?And then Exception https://code.sololearn.com/c3Btcqz3KJfc/?ref=app

3rd Nov 2020, 10:35 AM
Sergey
Sergey - avatar
5 Respostas
+ 2
It definitely fails at: int replaced = Int32.Parse(pi.ToString().Substring((i-k), (i - k + 1))); Changing to this prevents the crash but I didn't test that the digits of PI are calculated perfectly: int replaced = Int32.Parse(pi.ToString().Substring((i-k), 1)); Note that the second parameter of Substring is a length. It is not an end index. Maybe confusing these is your mistake. More details on the Substring method are at: https://docs.microsoft.com/en-us/dotnet/api/system.string.substring?view=netcore-3.1
3rd Nov 2020, 8:19 PM
Josh Greig
Josh Greig - avatar
+ 1
thanks, You are right)
4th Nov 2020, 3:42 AM
Sergey
Sergey - avatar
+ 1
And how to decide this problem?
4th Nov 2020, 3:52 AM
Sergey
Sergey - avatar
+ 1
I decided problem)) It was in this int replaced = Int32.Parse(pi.ToString().Substring((i-k), 1)); if (replaced == 9) { replaced = 0; } else { replaced++; } pi.ToString().Remove(i - k,1);
4th Nov 2020, 4:49 AM
Sergey
Sergey - avatar
+ 1
So algorithm very well, but it’s a simple and not quick
4th Nov 2020, 4:52 AM
Sergey
Sergey - avatar