C# :: Aufgabe #60 :: Lösung #3

9 Lösungen Lösungen öffentlich
#60

vollkommene Zahl (Mathematisches Problem)

Anfänger - C# von bibir - 03.09.2014 um 08:21 Uhr
Man nennt eine natürliche Zahl N "vollkommen", wenn die Summe aller echten Teiler von N gleich N ist.

Zum Beispiel ist 28 eine vollkommene Zahl, da 1 + 2 + 4 + 7 + 14 = 28 ist.

Schreibe ein Programm/Skript, das beliebig oft eine Zahl N (mit 0 < N < 100000) einliest und feststellt, ob N eine vollkommene Zahl ist. Falls ja, soll das Programm N und die zugehörenden echten Teiler ausgeben, andernfalls ist nur N und die Meldung "ERFUELLT DIE BEDINGUNG NICHT" zu drucken.
#3
vote_ok
von eulerscheZhl (5230 Punkte) - 21.11.2014 um 14:46 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Program
    {
        public static bool IsPerfect(int n)
        {
            List<int> divisors = GetDivisors(n);
            if (divisors.Sum() == n)
            {
                foreach (int divisor in divisors)
                    Console.WriteLine(divisor);
                return true;
            }
            return false;
        }
        
        public static List<int> GetDivisors(int n)
        {
            Dictionary<int, int> factors = GetPrimeFactors(n);
            List<int> divisors = new List<int>();
            divisors.Add(1);
            foreach (KeyValuePair<int, int> entry in factors)
            {
                int size = divisors.Count;
                int currFactor = entry.Key;
                for(int pow = 1; pow <= entry.Value; pow++)
                {
                    for(int i = 0; i < size; i++)
                        divisors.Add(divisors[i] * currFactor);
                    currFactor *= entry.Key;
                }
            }
            divisors.Remove(n);
            divisors.Sort();
            return divisors;
        }
        
        public static Dictionary<int, int> GetPrimeFactors(int n)
        {
            Dictionary<int, int> result = new Dictionary<int, int>();
            while (n % 2 == 0) 
            {
                AddFactor(2, ref result); 
                n /= 2;
            }
            int limit = (int)Math.Sqrt(n);
            for(int i = 3; i <= limit; i += 2)
            {
                while(n % i == 0)
                {
                    AddFactor(i, ref result);
                    n /= i;
                    limit = (int)Math.Sqrt(n);
                }
            }
            if (n > 0)
                AddFactor(n, ref result);
            return result;
        }
        
        public static void AddFactor(int factor, ref Dictionary<int, int> dict)
        {
            if (dict.ContainsKey(factor))
                dict[factor]++;
            else dict.Add(factor, 1);
        }
        
        public static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("Geben Sie eine Zahl ein: ");
                int n = int.Parse(Console.ReadLine());
                if (!IsPerfect(n))
                    Console.WriteLine("ERFUELLT DIE BEDINGUNG NICHT");
            }
        }
    }
}

Kommentare:

Für diese Lösung gibt es noch keinen Kommentar

Bitte melden Sie sich an um eine Kommentar zu schreiben.
Kommentar schreiben