C# :: Aufgabe #60
9 Lösungen

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.
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.
Lösungen:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace Vollkommene_Zahlen { class Program { static void Main(string[] args) { //Deklarationen ArrayList Teiler = new ArrayList(); int Eingelesene_Zahl; int Summe_Teiler = 0; //Einlesen einer Ganzzahl Console.WriteLine("Bitte eine Zahl eingeben, um diese auf Vollkommenheit zu überprüfen: "); while (!int.TryParse(Console.ReadLine(), out Eingelesene_Zahl)) Console.WriteLine("Bitte eine Ganzzahl eingeben!"); //Berechnung Teiler for(int i = 1; i<=Eingelesene_Zahl-1; i++) { if(Eingelesene_Zahl%i==0) Teiler.Add(i); } //Aufsummierung Teiler for(int i = 0; i<Teiler.Count; i++) { Summe_Teiler += Convert.ToInt32(Teiler[i]); } //Prüfung ob die Summe der Teiler der eingelesenen Zahl entspricht + Ausgabe if (Summe_Teiler == Eingelesene_Zahl) { Console.WriteLine("Die eingebene Zahl " + Eingelesene_Zahl + " ist eine vollkommene Zahl, da ihre Teiler: "); for (int i = 0; i < Teiler.Count; i++) { if(i==Teiler.Count-1) { Console.Write(Teiler[i]+ " "); } if (i==Teiler.Count-2) { Console.Write(Teiler[i]+ " und "); } if (i!=Teiler.Count-1 && i!=Teiler.Count-2) { Console.Write(Teiler[i]+ ", "); } } Console.WriteLine("aufsummiert " + Eingelesene_Zahl + " ergeben."); } else { Console.WriteLine("Die eingebene Zahl ist nicht vollkommen."); } Console.ReadLine(); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace VollkommeneZahlenErmitteln { class Program { static void Main(string[] args) { Console.WriteLine("Geben Sie eine natürliche Zahl grösser 0 und kleiner 100000 ein"); string input = Console.ReadLine(); int zahl = Convert.ToInt32(input); List<int> teiler = new List<int>(); for (int i = 1; i <= zahl / 2; i++) { int rest = zahl % i; if (rest == 0) teiler.Add(i); } int ergebnis = 0; for (int i = 0; i < teiler.Count; i++) { ergebnis += teiler[i]; } if (ergebnis == zahl) { for (int i = 0; i < teiler.Count; i++) { if (i != teiler.Count - 1) Console.Write(teiler[i].ToString() + " + "); else Console.Write(teiler[i].ToString() + " = "); } Console.Write(zahl); } else { Console.WriteLine(zahl + " ERFUELLT DIE BEDINGUNG NICHT"); } Console.ReadKey(); } } }

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"); } } } }

// ================================================== // Copyright 2014 (C) , dotLogix // Author: Alexander Schill <alexander@schillnet.de>. // ================================================== namespace VolkommeneZahl { #region Using Directives using System; using System.Collections.ObjectModel; using System.Linq; #endregion internal class Program { private static void Main(string[] args) { while(true) { Console.Write("Zahl eingeben: "); var n = int.Parse(Console.ReadLine()); var collection = new Collection<int>(); for(var i = 1; i < n; i++) if(n % i == 0) collection.Add(i); var sum = collection.Sum(); Console.WriteLine(sum == n ? string.Join("+", collection) + " = " + n : n + " erfüllt die Bedingung nicht"); } } } }
kurz und funktioniert :D

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; /* * 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. */ namespace VollkommeneZahl { class Program { static void Main(string[] args) { string antwort = "Y"; while (antwort[0] == 'Y') { int number; do { Console.WriteLine("Geben Sie eine Zahl zwischen 0 und 100000 an: "); } while (!int.TryParse(Console.ReadLine(), out number) || number < 0 || number > 100000); List<int> teiler = new List<int>(); for (int i = 1; i <= (number / 2); i++) { if (number % i == 0) { teiler.Add(i); } } int result = 0; foreach (int item in teiler) { result += item; } if (result == number) { Console.WriteLine("{0} ist eine vollkommene Zahl.\nDie Teiler: ", number); foreach (int item in teiler) { Console.Write("{0}, ", item); } } else { Console.WriteLine("{0} ist keine vollkommene Zahl.", number); } Console.WriteLine("\nNoch eine Zahl? (Y/N)"); antwort = Console.ReadLine().ToUpper(); if (antwort.Length < 1) { antwort = "N"; } } } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PerfekteZahl { class Program { static void Main(string[] args) { int zahl, summeDerTeiler=0, j=0; string[] teiler = new string[100000]; int laenge=0; Console.WriteLine("Zahlenprüfer: Vollkommende Zahlen"); do { do { Console.Clear(); summeDerTeiler = 0; Console.Write("Bitte geben Sie eine Zahl zwischen 0 und 100000 ein: "); zahl = Convert.ToInt32(Console.ReadLine()); } while(zahl < 0 || zahl > 100000); // Überprüfung auf richtige Eingabe for (int i=1;i<zahl;i++) { if (zahl % i == 0) { summeDerTeiler = summeDerTeiler + i; // summieren aller Teiler durch die Variable i teiler[j] = Convert.ToString(i); // speichern der einzelnen Teiler in Variable teiler j++; } } teiler[j] = "\0"; j = 0; if (zahl == summeDerTeiler) { Console.WriteLine('\n'+Convert.ToString(zahl) + " ist eine vollkommene Zahl"); Console.Write("echte Teiler sind: "); // Ausgabe der Teiler for (int i=0;teiler[i]!="\0";i++) { if (teiler[i+1]=="\0") Console.WriteLine(teiler[i]); else Console.Write(teiler[i]+" "); } } else { Console.WriteLine("\n"+Convert.ToString(zahl) + " ERFUELLT DIE BEDINGUNG NICHT"); } for (int i = 0; teiler[i] != "\0"; i++) laenge = i; //Variable teiler leeren... for (int i = 0; teiler[i] != "\0"; i++) teiler[i] = ""; Console.Write("\nWeitere Zahl überprüfen? (j/n): "); } while (Console.ReadLine().ToLower() == "j"); } } }

namespace Exercise_60 { using System; using System.Collections.Generic; using System.Linq; internal static class Program { private static void Main() { for (var i = 1; i < 100000; i++) { List<int> t = i.FindeTeiler(); if (i == t.Sum() - i) { Console.Write($"{i} | " + "{"); foreach (var teiler in t) { Console.Write($" {teiler} "); } Console.WriteLine("};"); } else { Console.WriteLine($"{i} ERFÜLLT DIE BEDINGUNG NICHT"); } } Console.Read(); } private static List<int> FindeTeiler(this int n) { List<int> teiler = new List<int>(); for (var i = 1; i <= n; i++) { if (n % i == 0) { teiler.Add(i); } } return teiler; } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TrainYourProgrammer60 { class Program { static void Main(string[] args) { Console.Write("Wollen Sie eine Überprüfung starten? "); while (Console.ReadLine() != "") { Console.Write("Geben Sie eine Zahl ein, die auf Ihre Vollkommenheit überprüft werden soll: "); int zahl = Convert.ToInt32(Console.ReadLine()); int vergleich = 0; string ausgabe = Convert.ToString(zahl) + ": "; for (int i = 1; i < zahl; i++) { if (zahl % i == 0) { vergleich += i; ausgabe += Convert.ToString(i) + " "; } } if (vergleich == zahl) { Console.WriteLine(ausgabe); } else { Console.WriteLine("{0} ERFUELLT DIE BEDINGUNG NICHT", zahl); } Console.Write("Wollen Sie eine weitere Zahl überprüfen? "); } } } }

static void VollkommeneZahl(int n) { List<int> echteTeiler = new List<int>(); if (n > 0 && n < 100000) { for (int i = 1; i <= n / 2; i++) { if (n % i == 0) { echteTeiler.Add(i); } } } if (echteTeiler.Sum() == n) { Console.WriteLine($"{n} ist eine vollkommene Zahl."); Console.WriteLine(string.Join(", ", echteTeiler.Select(x => x.ToString()).ToArray())); } else { Console.WriteLine($"{n} ist keine vollkommene Zahl."); } }