C# :: Aufgabe #299

3 Lösungen Lösungen öffentlich

Zwei-Quadrate-Satz (Fermat / Girard)

Anfänger - C# von JKooP - 06.04.2020 um 14:49 Uhr
1. Schreibe eine Funktion bzw. Methode mit der es möglich ist zu überprüfen, ob es sich bei der eingegebenen Zahl um eine Primzahl handelt.

2. Erweitere das Programm dahingehend, dass nur die ungeraden Primzahlen ausgegeben werden, welche dem Quadrate- Satz von
Fermat entsprechen. Also der Summe zweier ganzzahliger Quadrate.

Beispiele:
5 = 1² + 2², 13 = 2² + 3², 17 = 1² + 4², …

Gegenbeispiele:
3, 7, 11, 19 funktionieren nicht, da sie durch 4 geteilt (Modulo 4) nicht den Wert 1 ergeben. Daher gibt es auch keine Lösung (4n+1).

3. Erweitere das Programm wiederum, sodass die möglichen Lösungen aus als Zahlenpaare auf dem Bildschirm ausgegeben werden.
z.B.: 13 (2,3)

Viel Spaß

Lösungen:

vote_ok
von Waldgeist (2310 Punkte) - 07.04.2020 um 14:16 Uhr
Hallo,

hier die Lösung dieses interessanten Problems

Quellcode ausblenden C#-Code
using System;
using System.Windows.Forms;

namespace Aufgabe_299
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private bool TestPrimzahl(int Wert)
        {
            for (int i = 2; i <= Wert - 1; i++)
            {
                if (Wert % i == 0) return false;
            }
            return true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Meldung.Clear();
            textBoxbedingung.Clear();
            faktor1.Clear();
            faktor2.Clear();
            int zahl = Convert.ToInt32(zahlbox.Text);

            if (TestPrimzahl(zahl))
            {
                Meldung.Text = zahl + " ist eine Primzahl ! ";

                if (zahl % 4 == 1)
                {
                    textBoxbedingung.Text = "Bedienung erfüllt !";
                    int x = Convert.ToInt32(Math.Sqrt(zahl - 1));
                    int y = Convert.ToInt32(Math.Sqrt(zahl - x * x));

                    faktor1.Text = y.ToString();
                    faktor2.Text = x.ToString();
                }
                else textBoxbedingung.Text = "Bedingung nicht erfüllt !";
            }
            else
            {
                Meldung.Text = zahl + " ist keine Primzahl ! ";
            }
        }

       
    }
}

vote_ok
von luckyman81 (550 Punkte) - 15.04.2020 um 20:31 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;

namespace CS_Aufgabe_299_FermatQuadrate
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] fermatPrimes = new int[] { 5, 13, 17, 29 };
            int[] nFermatPrimes = new int[] { 3, 7, 11, 19 };

            Console.WriteLine($"{fermatPrimes.All(x => x.IsFermatPrime())}");
            Console.WriteLine($"{nFermatPrimes.All(x => (!x.IsFermatPrime()))}");

            List<int> fermats = new List<int>();
            for (int i = 0; i < 1e3; i++)
            {
                if (i.IsFermatPrime())
                {
                    fermats.Add(i);
                }
            }

            foreach (var fermat in fermats)
            {
                for (int i = 0; i < Math.Sqrt(1e3); i++)
                {
                    double cand = Math.Sqrt(fermat - Math.Pow(i, 2.0));

                    if (Math.Abs(cand - Math.Truncate(cand)) < 1e-10)
                    {
                        Console.WriteLine($"{fermat} ({i}, {cand})");
                        break;
                    }
                }
            }

            Console.ReadKey();
        }
    }

    public static class Extensions {

        public static bool IsPrime(int n)
        {
            if (n > 1)
            {
                return Enumerable.Range(1, n).Where(x => n % x == 0).SequenceEqual(new[] { 1, n });
            }
            return false;
        }

        public static bool IsFermatPrime(this int n)
        {
            if (!IsPrime(n)) return false;
            else if (n % 4 == 1) return true;
            else
                return false;
        }
    }
}
vote_ok
von JKooP (18090 Punkte) - 24.10.2020 um 11:03 Uhr
NET Core 3.x

Quellcode ausblenden C#-Code
using System;
using System.Linq;
using static System.Math;

namespace CS_Aufgabe_299_Zwei_Quadrate_Satz_Fermat
{
    class Program
    {
        static void Main() => Enumerable.Range(3, 1_000)
            .Where(x => x switch { 2 => true, var k when k > 2 => !Enumerable.Range(2, (int)Ceiling(Sqrt(x)) - 1).Any(g => x % g == 0), _ => false } && x % 4 == 1).ToList()
                .Select(i => new { id = i, x = (int)Sqrt(i - 1), y = (int)Sqrt(i - Pow((int)Sqrt(i - 1), 2)) }).ToList().ForEach(n => Console.WriteLine($"{n.id} -> { (n.y, n.x) }"));
    }
}

2114402

Du scheinst einen AdBlocker zu nutzen. Ich würde mich freuen, wenn du ihn auf dieser Seite deaktivierst und dich davon überzeugst, dass die Werbung hier nicht störend ist.