Programmierthread
07.07.2018 um 13:39Nachdem ich mir einen Knoten ins Hirn gedacht habe, ist es mir gelungen, ein Computerprogramm zusammenzuklamösern, mit welchem es 1. Möglich ist, zahlen in ihre Primfaktoren zu zerlegen (siehe die Methode factorize(), 2. den größten gemeinsamen Nenner (siehe findGCD(...)) und das kleinste gemeinsame Vielfach (siehe findLCM(...)) zu berechnen. Ich habe das in C# gemacht:
Wenn sonst noch jemand Bock auf Programmierung und so hat, dann nur her mit Ideen, Fragen, Code-Snippets ;)
using System;
using System.Collections;
namespace ConsoleApp1
{
class Program
{
private static ArrayList primeFactorStrings = new ArrayList();
private static ArrayList primes = new ArrayList();
private static ArrayList primes2 = new ArrayList();
private static ArrayList nonPrimes = new ArrayList();
private static ArrayList numbersToRemove = new ArrayList();
static void Main(string[] args)
{
factorize();
findGCD("2 2 2", "2 2");
findLCM("2 2 2", "2 2");
Console.ReadLine();
}
private static string findLCM(string num1, string num2)
{
ArrayList divWithoutExponents = new ArrayList();
int a = convertPrimeFactorStringToInt(num1);
int b = convertPrimeFactorStringToInt(num2);
Console.WriteLine(a);
int res = (a*b)/(Int32.Parse(findGCD(num1, num2)));
Console.WriteLine("lcm: " + res);
return res.ToString();
}
private static int convertPrimeFactorStringToInt(string numberStr)
{
int res = 1;
for(int i=0;i<numberStr.Split(' ').Length;i++){
res *= Int32.Parse(numberStr.Split(' ')[i].Trim());
}
return res;
}
private static string findGCD(string num1, string num2)
{
ArrayList divWithoutExponents = new ArrayList();
ArrayList exponents = new ArrayList();
foreach (string v in num1.Split(' '))
{
double d = Double.Parse(v);
if (num2.Contains(v.Trim()) && !divWithoutExponents.Contains(d))
{
divWithoutExponents.Add(d);
}
}
foreach (double v in divWithoutExponents)
{
exponents.Add(minNumberOfOccurrences((int)v, num1, num2));
}
double res = 1;
try
{
for (int i = 0; i < divWithoutExponents.Count; i++)
{
res *= Math.Pow((double)divWithoutExponents[i], (double)exponents[i]);
}
}
catch (IndexOutOfRangeException)
{
Console.WriteLine("Exponents could not be found properly.");
}
Console.WriteLine("gcd: " + res);
return res.ToString();
}
private static double minNumberOfOccurrences(int v, string num1, string num2)
{
int a = 0;
int b = 0;
foreach (string exp in num1.Split(' '))
{
if (Int32.Parse(exp.Trim()) == v)
{
a++;
}
}
foreach (string exp in num2.Split(' '))
{
if (Int32.Parse(exp.Trim()) == v)
{
b++;
}
}
double res = a < b ? a : b;
return res;
}
private static void factorize()
{
Console.WriteLine("I want to do prime factorization with the natural number you enter.");
Console.WriteLine("Enter e to exit.");
Console.WriteLine("");
var input = Console.ReadLine();
int number = 0;
try
{
if (input.Equals("e"))
{
Environment.Exit(0);
}
if (input.Equals("gcd"))
{
/**
* Here and in the following if: IprimeFactorStrings.Count == 0 -> findGCD("2 2 2", "2 2");
*
* else: I can only compute the gcd of TWO numbers.
*
*/
Environment.Exit(0);
}
if (input.Equals("lcm"))
{
/**
* Here and in the following if: IprimeFactorStrings.Count == 0 -> findLCM("2 2 2", "2 2");
*
* else: I can only compute the lcm of TWO numbers.
*
*/
Environment.Exit(0);
}
else {
number = Int32.Parse(input);
}
}
catch (System.FormatException)
{
Console.WriteLine("Please write a natural number, don't write a decimal or any gibberish.");
}
catch (System.OverflowException)
{
Console.WriteLine("The number you entered is so big that I cannot parse it.");
}
if (number == 0)
{
Console.WriteLine("Not yet implemented, see methods LCM() and GCD()!");
}
if (isPrime(number))
{
primes.Add(number);
Console.WriteLine("The result is: " + string.Join(" ", primes.ToArray()));
//Cleanup
primes.Clear();
primes2.Clear();
nonPrimes.Clear();
numbersToRemove.Clear();
factorize();
}
for (double i = 2; i < number; i++)
{
if (isNatural((double)number / i) && !isPrime((int)(number / i)))
{
nonPrimes.Add((double)number / i);
}
if (isNatural((double)number / i) && isPrime((int)(number / i)))
{
primes.Add((double)number / i);
}
}
foreach (double n in nonPrimes)
{
foreach (double p in primes)
{
if (!isNatural(n / p))
{
numbersToRemove.Add(p);//Add these numbers to a List numbersToRemove,
}
}
}
foreach (double v in primes)
{
primes2.Add(v);
}
foreach (double r in numbersToRemove)
{//And remove them from the original list.
primes2.Remove(r);
}
string result = string.Join(" ", primes.ToArray()) + " " + string.Join(" ", primes2.ToArray());
Console.WriteLine("The result is: " + result);
//Add it to primeFactorStrings
primeFactorStrings.Add(result);
//Cleanup
primes.Clear();
primes2.Clear();
nonPrimes.Clear();
numbersToRemove.Clear();
factorize();
}
private static bool isNatural(double v)
{
return (v % 1) == 0;
}
private static bool isPrime(int numberToTest)
{
if (numberToTest == 1 && numberToTest == 2)
{
return true;
}
for (double d = 2.0; d < numberToTest; d++)
{
if (numberToTest % d == 0)
{
return false;
}
}
return true;
}
/*
internal class ReverseSort : IComparer
{
public int Compare(object x, object y)
{
return Comparer.Default.Compare(y, x);
}
}
*/
}
}
Wenn sonst noch jemand Bock auf Programmierung und so hat, dann nur her mit Ideen, Fragen, Code-Snippets ;)