Nth Prime Approximation

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

Es gibt 5 Antworten in diesem Thema. Der letzte Beitrag () ist von Rikudo.

    Nth Prime Approximation

    Hey,

    Ich möchte gerne eine Funktion schreiben die die n-te primzahl zurückgibt.
    Dabei sollen aber nicht alle Primzahlen von 0 bis n berechnet werden sondern eine Vermutung berechnet werden.
    Also quasi wie hier :
    primes.utm.edu/nthprime/algorithm.php

    Ich hab versucht das nachzubauen aber bei mir kommt bullshit raus ..

    C#-Quellcode

    1. static void Main(string[] args)
    2. {
    3. int n = 4000;
    4. double a = Math.Log(n) + Math.Log(Math.Log(n)) -1+((Math.Log(Math.Log(n))-2)/(Math.Log(n)))-((Math.Pow((Math.Log(Math.Log(n))),2)-(6*Math.Log(Math.Log(n))+11/(2*Math.Pow(Math.Log(n),2))+(1/(Math.Pow(Math.Log(n),2))))));
    5. Console.WriteLine(a);
    6. Console.ReadKey();
    7. }
    C# Developer
    Learning C++
    @Rikudo Jou, die Klammersetzung in der Formel ist etwas suboptimal.
    Mal zur Anregung:
    Nutze folgende Routine, da lässt sich der Code leichter übertragen:

    C#-Quellcode

    1. private double loglog(double x)
    2. {
    3. return Math.Log(Math.Log(x));
    4. }
    Und wenn Du dann ne Form machst und die Werte mit nem NUD durchklickerst, wird das richtig freundlich.
    Ganz richtig isses allerdings immer noch nicht:

    C#-Quellcode

    1. private void numericUpDown1_ValueChanged(object sender, EventArgs e)
    2. {
    3. this.label1.Text = prim((double)this.numericUpDown1.Value).ToString();
    4. }
    5. double prim(double n)
    6. {
    7. return n * (log(n) + loglog(n) - 1 + (loglog(n) - 2) / log(n) - Math.Pow((loglog(n)), 2) - 6 * loglog(n) + 11) / (2 * log(n) * log(n));
    8. }
    9. private double log(double x)
    10. {
    11. return Math.Log(x);
    12. }
    13. private double loglog(double x)
    14. {
    15. return Math.Log(Math.Log(x));
    16. }
    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!
    Vollzitat entfernt. ~Trade

    Hi,
    Danke für die Antwort, aber das scheint nich nicht ganz zu stimmen...

    C#-Quellcode

    1. class Program
    2. {
    3. static void Main(string[] args)
    4. {
    5. double n = 3000;
    6. double a = prim(n);
    7. Console.WriteLine(a);
    8. Console.ReadKey();
    9. }
    10. static double prim(double n)
    11. {
    12. return n * (log(n) + loglog(n) - 1 + (loglog(n) - 2) / log(n) - Math.Pow((loglog(n)), 2) - 6 * loglog(n) + 11) / (2 * log(n) * log(n));
    13. }
    14. private static double log(double x)
    15. {
    16. return Math.Log(x);
    17. }
    18. private static double loglog(double x)
    19. {
    20. return Math.Log(Math.Log(x));
    21. }
    22. }


    Bei 3000 ist der Output 76.935...
    Die 3000te vermutete Primzahl ist aber 27,449.

    Was ist noch falsch am Algorithmus?
    C# Developer
    Learning C++

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Trade“ ()

    Ist es möglich das es so heissen muss? Ich kann leider den Link nicht aufmachen und prüfen, da bei mir eine strenge sicherheitsmeldung schreit.

    C#-Quellcode

    1. private double prim(double n)
    2. {
    3. //Return n * (log(n) + loglog(n) - 1.0 + (loglog(n) - 2.0) / log(n) - Math.Pow((loglog(n)), 2.0) - 6.0 * loglog(n) + 11) / (2.0 * log(n) * log(n))
    4. return n * (log(n) + loglog(n) - 1.0 + (loglog(n) - 2.0) / log(n) - (Math.Pow((loglog(n)), 2.0) - 6.0 * loglog(n) + 11.0) / (2.0 * (log(n) * log(n))));
    5. }

    Rikudo schrieb:

    aber das scheint nich nicht ganz zu stimmen...

    RodFromGermany schrieb:

    Ganz richtig isses allerdings immer noch nicht:
    Das ist mir wohl bewusst. Alerdings finde ich keinen Fehler in der Formel.
    Möglicherweise ist tatsächlich bei der Seite die eine oder die andere Klammer falsch gesetzt. :/
    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!