Permutácia alebo poradie základného súboru n prvkov je skupina všetkých n prvkov, pri ktorej záleží na poradí prvkov v nej (pričom toto poradie môže byť ľubovoľné). Ako permutácia alebo premiestnenie sa označuje aj proces vytvorenia takejto skupiny. Slovo permutovať znamená obmieňať. Rozlišujeme premutácie s opakovaním a bez opakovania. Permutácie bez opakovania M je množina n rôznych prvkov, z ktoých tvoríme n - tice, pričom prvky v n - ticiach sa nemôžu opakovať. P(n) = n!, kde n! označuje faktoriál. Ak sa nehovorí inak, sú permutácie myslené bez opakovania. Príklad 1. Máme skupinu troch rôznych prvkov a,b,c. Permutácie týchto prvkov predstavujú skupiny abc, acb, bac, bca, cab, cba. Ich počet je teda: P(3) = 3! = 6 2. Koľkými spôsobmi môžno rozsadiť troch žiakov A, B, C v lavici? Riešenie: Permutácie s opakovaním M je množina n prvkov, z ktorých je k1 rovnakých 1. druhu, k2 je rovnakých 2. druhu, až kr je rovnakých r - tého druhu, pričom platí: k1 + k2 + ... + kr = n. Prvky vo výbere sa teda môžu opakovať. Počet permutácií s opakovaním je určený ako: , Príklad 1. Máme skupinu troch prvkov a,a,b. Skupina je teda zložená z dvoch skupín (teda k = 2), pričom prvá skupina má dva prvky a, tzn. k1 = 2, a druhá skupina obsahuje jeden prvok b, tzn. k2 = 1. Permutáciami s opakovaním získame skupiny aab, aba, baa. Počet týchto skupín je tedy rovný: 2. Koľkými spôsobmi možno rozsadiť 8 žiakov, z ktorých majú dvaja zelené, traja červené a ďalší traja modré vetrovky? Riešenie: Modulo Modulo (Modulárna aritmetika) je systém aritmetiky pre celé čísla, ktoré sa menia až kým nedosiahnu určitú hodnotu, modulus. Modulárna aritmetika bola uvedená Carlom Friedrichom Gaussom v jeho knihe Disquisitiones Arithmeticae publikovanej v roku 1801. Príklad A mod B = celočíselný zvyšok po delení čísla A číslom B. 7 mod 5 = 2; {7/5 = 1,4} Vezmeme celočíselnú časť 1,4 po zaokrúhlení smerom dole (TRUNC) = 1 Vynasobime 1 x 5 = 5 7 - 5 = 2 => MOD Ine pr.: 1 MOD 2 MOD 3 MOD 4 MOD 5 MOD 6 MOD ... 4 4 4 4 4 4 = = = = = = 1; 2; 3; 0; //Začne modulo cyklus 1; 2; Syntax MOD (number,divisor) Number. Divisor. Číslo, ku ktorému chcete nájsť zvyšok po delení. Číslo, ktorým chcete deliť číslo. Poznámky Ak je deliteľ 0, funkcia MOD vráti chybovú hodnotu #DELENIE NULOU!. Funkciu MOD možno vyjadriť pomocou funkcie INT: MOD(n, d) = n - d*INT(n/d) Príklady Vzorec Popis (výsledok) =MOD(3, 2) Zvyšok po delení 3/2 (1) =MOD(-3, 2) Zvyšok po delení -3/2. Výsledok má to isté znamienko ako deliteľ (1) =MOD(3, -2) Zvyšok po delení 3/-2. Výsledok má to isté znamienko ako deliteľ (-1) =MOD(-3, -2) Zvyšok po delení -3/-2. Výsledok má to isté znamienko ako deliteľ (-1) Funkcia XOR XOR Logická operácia XOR(exclusive or) je binárna logická operácia. Pravdivostná hodnota je jedna práve vtedy , keď práve jeden z argumentov je jedna. Viď. obrázok. Hradlo XOR je jedným zo základných kombinačných logických obvodov, ktorého výstup je exkluzívny logický súčet vstupov („buď A, alebo B“). Výstup je log.1 vtedy a len vtedy ak sa hodnoty vstupov líšia. XOR je funkciou súčtu modulo 2, využíva sa preto v binárnych sčítačkách v aritmetickologických jednotkách počítačov. Polovičná sčítačka pozostáva z jedného hradla XOR (súčet) a jedného hradla AND (prenos do vyššieho rádu). XOR je binárna operácia - je definovaná na dvoch vstupoch. V elektronike sa ale bežne používa pojem hradla XOR pre 3 a viac vstupov. Najčastejšia interpretácia je taká, že prvé dva vstupy sú privedené do prvého hradla XOR. Každý ďalší vstup je privedený spolu s výstupom predchádzajúceho hradla na vstup ďalšieho hradla XOR. Výsledkom je obvod, ktorého výstup je log.1 vtedy a len vtedy ak je log.1 na nepárnom počte vstupov. Takýto obvod sa využíva ako generátor parity. Možná je aj druhá interpretácia, vychádzajúca z pojmu „exkluzívny“ logický súčet ako aj z IEC značky hradla XOR (viď obr. vpravo). Podľa tejto interpretácie je výstup log.1 vtedy a len vtedy, ak práve na jednom z N vstupov je log.1, čo by zodpovedalo aj „=1“ v značke IEC. IEC značka však nebola myslená pre širšie než 2-vstupové hradlo a takéto jej rozšírenie nie je platné. Táto interpretácia viacvstupového hradla XOR nie je v praxi častá, nakoľko sčítačky a generátory parity sú častejšie než detektory „1 z N“. Modulo operation Quotient (red) and remainder (green) functions using different algorithms. In computing, the modulo operation finds the remainder of division of one number by another. Given two positive numbers, a (the dividend) and n (the divisor), a modulo n (abbreviated as a mod n) can be thought of as the remainder, on division of a by n. For instance, the expression "7 mod 3" would evaluate to 1 because after dividing 7 by 3 (=2) the subtraction of 6 (=2*3) from 7 leaves 1, while "9 mod 3" would evaluate to 0 because the division of 9 by 3 is even, there is nothing to subtract from 9 after multiplying 3 times 3. (Notice that doing the division with a calculator won't show you the result referred to here by this operation.) When either a or n are negative, this naive definition breaks down and many programming languages differ in how these values are defined. Although typically performed with a and n both being integers, many computing systems allow other types of numeric operands. See modular arithmetic for an older and related convention applied in number theory. Remainder calculation for the modulo operation Integer modulo operators in various programming languages Language Operator Result has the same sign as % Dividend mod Divisor rem Dividend ASP Mod Not defined ALGOL-68 %× Always positive AppleScript mod Dividend BASIC Mod Not defined bc % Dividend bash % Dividend C (ISO 1990) % Implementation defined C (ISO 1999) % Dividend C++ % Implementation defined[1] C# % Dividend CLARION % Dividend Clojure mod Divisor ColdFusion % Dividend mod Divisor rem Dividend D % Dividend[2] Eiffel \\ Dividend ActionScript Ada Common Lisp rem Dividend mod Divisor remainder Dividend Mod Divisor mod Dividend modulo Divisor GML (Game Maker) mod Dividend Go % Dividend mod Divisor rem Dividend J |~ Divisor Java % Dividend JavaScript % Dividend Just Basic MOD Dividend Lua % Divisor Erlang Euphoria FileMaker Fortran Haskell Liberty Basic MOD Dividend MathCad mod(x,y) Divisor Maple (software) e mod m Dividend Mathematica Mod Divisor Microsoft Excel =MOD() Divisor mod Divisor rem Dividend MATLAB Oberon MOD Divisor Objective Caml mod Dividend Occam \ Dividend Pascal (Delphi) mod Dividend Perl % Divisor[1] PHP % Dividend PL/I mod Divisor (ANSI PL/I) PowerBuilder mod(x,y) ? mod Divisor rem Dividend Python % Divisor RealBasic MOD Dividend R %% Divisor RPG %REM Dividend Ruby % Divisor modulo Divisor remainder Dividend mod Always Nonnegative (Euclidean) mod0 Closest to zero modulo Divisor rem Dividend Prolog (ISO 1995) Scheme Scheme R6RS[2] SenseTalk Smalltalk \\ Divisor SQL (SQL:1999) mod(x,y) Dividend mod Divisor Int.rem Dividend % Divisor Standard ML Tcl Torque Game % Engine Dividend fPart(x/y)*y Dividend TI-BASIC Turing (programming mod language) Divisor Verilog (2001) % Dividend mod Divisor rem Dividend Mod Dividend VHDL Visual Basic x86 Assembly IDIV Dividend Floating-point modulo operators in various programming languages Language C (ISO 1990) C (ISO 1999) C++ Operator Result has the same sign as fmod ? fmod Dividend remainder Closest to zero std::fmod ? % Dividend mod Divisor rem Dividend Go math.Fmod Dividend Haskell (GHC) Data.Fixed.mod' Divisor Java % Dividend JavaScript % Dividend Objective mod_float Caml Dividend C# Common Lisp Perl POSIX::fmod Dividend PHP fmod Dividend % Divisor math.fmod Dividend % Divisor flmod Always Nonnegative (Euclidean) flmod0 Closest to zero Python Ruby Scheme R6RS Standard Real.rem ML Dividend There are various ways of defining a remainder, and computers and calculators have various ways of storing and representing numbers, so what exactly constitutes the result of a modulo operation depends on the programming language and/or the underlying hardware. In nearly all computing systems, the quotient q and the remainder r satisfy This means there are two possible choices for the remainder, one negative and the other positive, and there are also two possible choices for the quotient. Usually, in number theory, the positive remainder is always chosen, but programming languages choose depending on the language and the signs of a and n.[3] However, Pascal and Algol68 do not satisfy these conditions for negative divisors, and some programming languages, such as C89, don't even define a result if either of n or a is negative. See the table for details. a modulo 0 is undefined in the majority of systems, although some do define it to be a. Many implementations use truncated division where the quotient is defined by truncation q = trunc(a/n) and the remainder by r=a-n q. With this definition the quotient is rounded towards zero and the remainder has the same sign as the dividend. Knuth[3] described floored division where the quotient is defined by the floor function q=floor(a/n) and the remainder r is Here the quotient rounds towards negative infinity and the remainder has the same sign as the divisor. Raymond T. Boute[4] introduces the Euclidean definition which is consistent with the division algorithm. Let q be the integer quotient of a and n, then: Two corollaries are that As described by Leijen,[5] Boute argues that Euclidean division is superior to the other ones in terms of regularity and useful mathematical properties, although floored division, promoted by Knuth, is also a good definition. Despite its widespread use, truncated division is shown to be inferior to the other definitions. Common Lisp also defines round- and ceiling-division where the quotient is given by q=round(a/n), q=ceil(a/n). IEEE 754 defines a remainder function where the quotient is a/n rounded according to the round to nearest convention. Common pitfalls When the result of a modulo operation has the sign of the dividend, it can sometimes lead to surprising mistakes: For example, to test whether an integer is odd, one might be inclined to test whether the remainder by 2 is equal to 1: bool is_odd(int n) { return n % 2 == 1; } But in a language where modulo has the sign of the dividend, that is incorrect, because when n (the dividend) is negative and odd, n % 2 returns -1, and the function returns false. One correct alternative is to test that it is not 0 (because remainder 0 is the same regardless of the signs): bool is_odd(int n) { return n % 2 != 0; } Modulo operation expression Some calculators have a mod() function button, and many programming languages have a mod() function or similar, expressed as mod(a, n), for example. Some also support expressions that use "%", "mod", or "Mod" as a modulo or remainder operator, such as a % n or a mod n or equivalent, for environments lacking a mod() function a - (n * int(a/n)) Performance issues Modulo operations might be implemented such that a division with a remainder is calculated each time. For special cases, there are faster alternatives on some hardware. For example, the modulo of powers of 2 can alternatively be expressed as a bitwise AND operation: x % 2n == x & (2n - 1). Examples (assuming x is an integer): x % 2 == x & 1 x % 4 == x & 3 x % 8 == x & 7 In devices and software that implement bitwise operations more efficiently than modulo, these alternative forms can result in faster calculations. Optimizing C compilers generally recognize expressions of the form expression % constant where constant is a power of two and automatically implement them as expression & (constant-1). This can allow the programmer to write clearer code without compromising performance. (Note: This will not work for the languages whose modulo have the sign of the dividend (including C), because if the dividend is negative, the modulo will be negative; however, expression & (constant-1) will always produce a positive result. So special treatment has to be made when the dividend is negative.) In some compilers, the modulo operation is implemented as mod(a, n) = a - n * floor(a / n). When performing both modulo and division on the same numbers, one can get the same result somewhat more efficiently by avoiding the actual modulo operator, and using the formula above on the result, avoiding an additional division operation.