Recent Changes - Search:

Oktatás

* Programozás 2
  + feladatsor
  + C feladatsor
  + Python feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

* Adator. prog.
  + feladatsor
  + quick link

Teaching

* Prog. for Data Sci.
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2025/26/1
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Scala


[ edit | logout ]
[ sandbox | passwd ]

GCD and LCM

See also std.numeric.

Notes:

  • GCD (greatest common divisor) in Hungarian: "legnagyobb közös osztó" (LNKO)
  • LCM (least common multiple) in Hungarian: "legkisebb közös többszörös" (LKKT)
(1) GCD and LCM
import std.stdio;
import std.numeric;

void main()
{
    writeln(gcd(30, 45)); // 15

    writeln(lcm(3, 4)); // 12
}

As you can see, these functions can be called with 2 arguments only. But what if we want to calculate the GCD or LCM of more values?

(2) multiple arguments
#!/usr/bin/env rdmd

import std.stdio;
import std.numeric;

int myLcm(const int[] numbers) pure
{
    assert(numbers.length >= 2);
    int result = lcm(numbers[0], numbers[1]);
    for (int i = 2; i < numbers.length; ++i)
    {
        result = lcm(result, numbers[i]);
    }
    return result;
}

int myGcd(const int[] numbers) pure
{
    assert(numbers.length >= 2);
    int result = gcd(numbers[0], numbers[1]);
    for (int i = 2; i < numbers.length; ++i)
    {
        result = gcd(result, numbers[i]);
    }
    return result;
}

void main()
{
    writeln(myLcm([4, 5, 5, 2])); // 20

    writeln(myGcd([30, 45, 120])); // 15
}
Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2025 August 23, 12:55