C# :: Aufgabe #136
8 Lösungen
Bestimmung des kleinsten gemeinsamen Vielfachen (kgV)
Anfänger - C#
von Nachbar
- 09.07.2016 um 10:08 Uhr
Schreibe ein Programm zur Bestimmung des kgV zweier natürlicher Zahlen (siehe Beispiel zur kgV-Berechnung). Die Berechnung soll ohne Beteiligung des größten gemeinsamen Teilers erfolgen.
Beispielausgabe:
Zahl 1: 5
Zahl 2: 7
kgV: 35
Beispielausgabe:
Zahl 1: 5
Zahl 2: 7
kgV: 35
Lösungen:
using System;
namespace kgv
{
class Program
{
static void Main()
{
Random r = new Random();
Int32 z1 = r.Next(100);
Int32 z2 = r.Next(100);
Int32[] array1 = new Int32[z2];
Int32[] array2 = new Int32[z1];
for(var i = 0; i < array1.Length; i++)
array1[i] = (i + 1) * z1;
for(var i = 0; i < array2.Length; i++)
array2[i] = (i + 1) * z2;
for (var i = 0; i < array1.Length; i++)
for(var j = 0; j < array2.Length; j++)
if(array1[i] == array2[j])
{
Console.WriteLine("kgV({0},{1}) = {2}", z1, z2, array1[i]);
i = array1.Length;
j = array2.Length;
}
Console.ReadKey();
}
}
}
using System;
public class MainClass
{
private static long Lcm(long a, long b) {
long lcm = 1;
for (int i = 2; i * i <= Math.Max (a, b); i++) { //mögliche Teiler
while (a % i == 0 || b % i == 0) { //wenn i Teiler ist
lcm *= i; //in kgV berücksichtigen
if (a % i == 0)
a /= i;
if (b % i == 0)
b /= i;
}
}
lcm *= a; //wenn der größte Primfaktor nur einmal vorkommt, ist a != 1
if (a != b)
lcm *= b;
return lcm;
}
public static void Main (string[] args)
{
Console.Write ("Zahl 1: ");
long a = long.Parse (Console.ReadLine ());
Console.Write ("Zahl 2: ");
long b = long.Parse (Console.ReadLine ());
Console.WriteLine ("kgV: " + Lcm (a, b));
}
}using System;
namespace ConsoleKGV
{
class Program
{
static void Main(string[] args)
{
Console.Write("1. Zahl: ");
int zahl1 = int.Parse(Console.ReadLine());
Console.Write("2. Zahl: ");
int zahl2 = int.Parse(Console.ReadLine());
int kgv = GetLowestCommenMultiple(zahl1, zahl2);
Console.WriteLine("KgV: " + kgv);
Console.ReadLine();
}
static int GetLowestCommenMultiple(int a, int b)
{
int big;
if (a == b)
{ return a; }
else if (a > b)
{
big = a;
}
else
{
big = b;
}
for (int i = big; i < a * b; i++)
{
if (i % a == 0 && i % b == 0)
{ return i; }
}
return a * b;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace kleinster_gemeinsamer_Vielfacher
{
class Program
{
static void Main(string[] args)
{
Random zufall = new Random();
int zahl1 = zufall.Next(1,101);
int zahl2 = zufall.Next(1,101);
static int kgV(int x, int y)
{
int kleine;
if (x > y)
{kleine = y; grosse = x;}
else
{kleine = x; grosse = y;}
for (i = kleine; i < x * y; i += kleine) //inkrementiert i mit der kleinen Zahl, da nur vielfache dieser berücksichtigt werden müssen
{
if (i % grosse == 0 && i != grosse) //da i immer Teiler von der kleinen Zahl ist, muss nur auf die grosse abgefragt werden
{ //&& i!=grosse da ich nur echte Vielfache suche, also ohne das eine Zahl sein eigener Vielfacher sein kann
return i;
}
return x * y;
}
}
Console.Write ("Zahl 1: " + zahl1);
Console.Write ("Zahl 2: " + zahl2);
Console.WriteLine ("kgV: " + kgV(zahl1, zahl2);
}
}
}
using System;
namespace kgV
{
class kgVProgram
{
static void Main(string[] args)
{
{
int kz, gz, t, i;
kz = Convert.ToInt32(Console.ReadLine());
gz = Convert.ToInt32(Console.ReadLine());
if (kz > gz)
{
t = kz;
kz = gz;
gz = t;
}
for (i=1,t=1;t!=kz*gz;i++)
{
t = kz * i;
if (t == gz)
break;
}
Console.WriteLine("KgV: {0}", t);
Console.ReadLine();
}
}
}
}using System;
namespace kgV
{
class kgVProgram
{
static void Main(string[] args)
{
{
int z1, z2, i, ii, e, e2;
z1 = Convert.ToInt32(Console.ReadLine());
z2 = Convert.ToInt32(Console.ReadLine());
for (ii = i = 2, e=z1, e2=z2; e != e2;)
{
if (e < e2)
e = z1 * i++;
else
e2 = z2 * ii++;
}
Console.WriteLine("KgV: {0}", e);
Console.ReadLine();
}
}
}
}
class kgV
{
public void rechnen()
{
Console.WriteLine("Geben Sie zwei Zahlen ein");
Console.WriteLine("Geben Sie die erste Zahl ein");
int zahl1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Geben Sie die zweite Zahl ein");
int zahl2 = Convert.ToInt32(Console.ReadLine());
int i = 1;
int j = 1;
while(zahl1*i != zahl2*j)
{
if ((zahl1*i) > (zahl2*j))
{
j++;
}
else if(zahl2*j > zahl1*i)
{
i++;
}
}
Console.WriteLine("Der kgV ist {0}", zahl1*i);
}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace kleinstes_gemeinsammes_Vielfaches
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("X eingeben");
int X = Int32.Parse(Console.ReadLine());
int y = X;
Console.WriteLine("Y eingeben");
int Y = Int32.Parse(Console.ReadLine());
int x = Y;
for(int x_ = 1; x_ < X; x_++)
{
for (int y_ = 1; y_ < Y; y_++)
{
if (y_ * X == x_ * Y)
{
//Console.WriteLine(x_ + " " + y_);
y = x_;
x = y_;
break;
}
}
}
Console.WriteLine(x * X + " ist das gleinze Gemeinsamme Vielfache");
Console.Read();
}
}
}