C# :: Aufgabe #287

3 Lösungen Lösungen öffentlich

Die Collatz-Sequenz (3n+1)

Anfänger - C# von DragStar - 23.03.2020 um 09:34 Uhr
Schreibe ein Programm, welches die Eingabe einer Zahl anfordert und dann von dieser Zahl die Collatz-Sequenz ausgibt.

Lösungen:

vote_ok
von Waldgeist (2310 Punkte) - 30.03.2020 um 10:28 Uhr
Hallo,

hier meine Version. Das Projekt findet Ihr als ZIP Datei im Anhang.

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

namespace Collatz_Reihe
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void rechen_Click(object sender, RoutedEventArgs e)
        {
            {
                ulong n = Convert.ToUInt64(eingabebox.Text);

                string ausgabe = n.ToString() + "  ";

                while (n > 1)
                {
                    if (n % 2 == 0)
                    {
                        n = n / 2;
                        ausgabe = ausgabe + n.ToString() + "  ";
                    }
                    else
                    {
                        n = 3 * n + 1;
                        ausgabe = ausgabe + n.ToString() + "  ";
                    }
                }

                ausgabebox.Text = ausgabe;
            }
        }
    }
}



Hier der XAML:
Quellcode ausblenden C#-Code
<Window x:Class="Collatz_Reihe.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Collatz_Reihe"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="450">
    <Grid>
        <Label Content="Program zur Ermittlung der Collatz-Reihe." HorizontalAlignment="Left" Height="39" Margin="20,10,0,0" VerticalAlignment="Top" Width="751" />
        <Label Content="Bitte geben Sie eine natürliche Zahl größer 0 ein  (n>0)." HorizontalAlignment="Left" Height="55" Margin="20,54,0,0" VerticalAlignment="Top" Width="751" />
        <TextBox x:Name="eingabebox" HorizontalAlignment="Left" Height="28" Margin="338,54,0,0" Text="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="83" TextAlignment="Left" />
        <TextBox x:Name="ausgabebox" HorizontalAlignment="Left" Height="132" TextWrapping="Wrap" VerticalAlignment="Top" Width="398" Margin="23,236,0,0" />
        <Label Content="Hier wird die Collatz-Reihe ausgegeben:" HorizontalAlignment="Left" Height="37" Margin="23,180,0,0" VerticalAlignment="Top" Width="398" />
        <Button x:Name="rechen" Content="Berechne" HorizontalAlignment="Left" Height="60" Margin="23,109,0,0" VerticalAlignment="Top" Width="398" Click="rechen_Click" />
    </Grid>
</Window>

vote_ok
von JKooP (18090 Punkte) - 01.04.2020 um 09:29 Uhr
NET Core 3.x

Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;

namespace CS_Aufage_287_Collatz
{
    class Program
    {
        static void Main(string[] args)
        {
            var c = Collatz(19);
            Console.WriteLine(string.Join(" | ", c));
            Console.WriteLine();
            CollatzR(19);
            
        }

        // Iterativ (Zahlen von 1 ...):

        public static IEnumerable<int> Collatz(int n)
        {
            // Bedingung 1: n > 0
            if (n < 1)
                yield break;

            // Startwert an Collection übergeben
            yield return n;

            do
            {
                // Bedingung 2: n ist gerade => n/2
                if (n % 2 == 0)
                    n /= 2;

                // Bedingung 3: n ist ungerade => 3n+1
                else
                    n = 3 * n + 1;

                // Wert an Collection übergeben
                yield return n;

            } while (n > 1);
            
        }

        // Rekursiv (Zahlen größer 1):

        public static void CollatzR(int n)
        {
            Console.Write($"{n} | ");

            if (n > 1) 
            {
                if (n % 2 == 0)
                    CollatzR(n / 2);
                else
                    CollatzR(3 * n + 1);
            }
        }
    }
}
vote_ok
von vinceee (180 Punkte) - 03.04.2020 um 20:43 Uhr
Quellcode ausblenden C#-Code

using System;

namespace CollatzProblem
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Geben Sie eine Zahl ein.");
            int Eingabe = Convert.ToInt32(Console.ReadLine());

            bool Gerade(int zahl)                                               //Kontrolle ob die Zahl gerade ist
            {
                return Eingabe % 2 == 0;
            }

            while (Eingabe != 1)                                                //So lange die letze Zahl nicht berechnet wurde, also 1
            {
                Console.Write("{0}, ", Eingabe);                                
                
                if(Gerade(Eingabe))                                             //Wenn die Zahl gerade ist
                {
                    Eingabe = Eingabe / 2;
                }
                else                                                            //Ansonsten
                {
                    Eingabe = 3 * Eingabe + 1;
                }
            }

            Console.Write(Eingabe);                                             //Die letzte 1 ausgeben

            Console.ReadKey();
        }
    }
}
1814112

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.