Nth Pi Digit

  • C#
  • .NET (FX) 1.0–2.0

Es gibt 21 Antworten in diesem Thema. Der letzte Beitrag () ist von RodFromGermany.

    Rikudo schrieb:

    optimieren
    So was, Du übergibst die Anzahl der Nachkommastellen und bekommst das ganze Feld zurück:
    Spoiler anzeigen

    C#-Quellcode

    1. static int[] CalculatePiArr(int anzahlStellen)
    2. {
    3. int[] result = new int[anzahlStellen + 1];
    4. int Indexcount = 0;
    5. int digits = anzahlStellen + 2;
    6. int[] x = new int[digits * 3 + 2];
    7. int[] r = new int[digits * 3 + 2];
    8. for (int j = 0; j < x.Length; j++)
    9. {
    10. x[j] = 20;
    11. }
    12. int prev = 0;
    13. for (int i = 0; i < digits; i++)
    14. {
    15. int carry = 0;
    16. for (int j = 0; j < x.Length; j++)
    17. {
    18. int num = (int)(x.Length - j - 1);
    19. int dem = num * 2 + 1;
    20. x[j] += carry;
    21. int q = x[j] / dem;
    22. r[j] = x[j] % dem;
    23. carry = q * num;
    24. }
    25. // calculate the digit, but don't add to the list right away:
    26. int digit = (int)(x[x.Length - 1] / 10);
    27. // handle overflow:
    28. if (digit >= 10)
    29. {
    30. digit -= 10;
    31. prev++;
    32. }
    33. if (i > 0)
    34. {
    35. result[Indexcount] = prev;
    36. Indexcount++;
    37. }
    38. // store the digit for next time, when it will be the prev value:
    39. prev = digit;
    40. r[x.Length - 1] = x[x.Length - 1] % 10; ;
    41. for (int j = 0; j < x.Length; j++)
    42. {
    43. x[j] = r[j] * 10;
    44. }
    45. }
    46. return result;
    47. }
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!