Discrete Math Chapter 4

Discrete Math Chapter 4

  1. 最大公约数

  2. 埃拉托斯特尼 计算100以内的素数

  3. 命令式阶乘,声明式阶乘

  4. Caesar Encryption

<?php
echo "<br>";
//欧几里得算法 求大公约数

function gcb($a, $b) {

    if ($a > $b) {
        $x = $a;
        $y = $b;
    } elseif ($a == $b) {
        return "Please Input Diff Numb";
    } else {
        $x = $b;
        $y = $a;
    }

    while ($y != 0) {
        $r = $x % $y;
        $x = $y;
        $y = $r;
    }

    return $x;
}

$a = 252;
$b = 198;
echo "欧几里得算法 求最大公约数 <br>";
echo gcb($a, $b);

?>

<?php 


echo "<br>";
//埃拉托斯特尼 计算100以内的素数

$num = [];
foreach (range(1, 100) as $number) {
    array_push($num, $number);
}

function prime($num)
{
    $prime = [];

    array_push($prime, 2, 3, 5, 7);

    for ($i = 1; $i < count($num); $i++) {
        //if ($num[$i] % 2 != 0 && $num[$i] % 3 != 0 && $num[$i] % 5 != 0 && $num[$i] % 7 != 0) {
        if (!($num[$i] % 2 == 0 || $num[$i] % 3 == 0 || $num[$i] % 5 == 0 || $num[$i] % 7 == 0)) {
            array_push($prime, $num[$i]);
        }
    }

    return $prime;
}

prime($num);


foreach (prime($num) as $key => $num) {
    echo "{$key} is {$num} <br>";
}


?>

<?php
//命令式范式阶乘 
function orderFactorial($n) {

    if ($n == 0 ) {
        return 1;
    } else {

        $result = 1;
            
        for ($i = $n; $i > 1; $i--) {
            $result *= $i;
        }

        return $result;
    }
}

$n = 10;
echo "Order Factorial";
echo orderFactorial($n);

//声明式范式阶乘

function declarationFactorial($n) {
    if ($n == 0) {
        return 1;
    } else {
        return $result = $n * declarationFactorial($n - 1);
    }
}

$n = 10;
echo "Declaration Factorial";
echo declarationFactorial($n);


?>


<?php

//古典加密
//Encery f(p) = (p + k) mod 26  f(p) = (p - k) mod 26;

$alphabet =  [
    "a",
    "b",
    "c",
    "d",
    "e",
    "f",
    "g",
    "h",
    "i",
    "j",
    "k",
    "l",
    "m",
    "n",
    "o",
    "p",
    "q",
    "r",
    "s",
    "t",
    "u",
    "v",
    "w",
    "x",
    "y",
    "z",
];


function caeserEncryption($string, $n, $alphabet) {

    $splitString = preg_split("//", $string , 0, PREG_SPLIT_NO_EMPTY);
    $getIndex = [];
    $result = [];

    for ($i = 0; $i < count($splitString); $i++) {
        $indexAlphbet = array_search($splitString[$i], $alphabet);
        array_push($getIndex, $indexAlphbet);
    }

    for ($i = 0; $i < count($getIndex); $i++) {
        $encryption =  ((int)$getIndex[$i] + (int)$n ) % 26;
        array_push($result, $encryption);
    }

    return $result;

}

$string = "nicetomeetyou";
$n = "3";

var_export(caeserEncryption($string, $n, $alphabet)) ;

$encryption = caeserEncryption($string, $n, $alphabet);

var_dump($encryption);

function caeserDecryption($encryption, $n, $alphabet) {
    $getIndex = [];
    $result = [];
    
    for ($i = 0; $i < count($encryption); $i++) {
        $decryption = ((int)$encryption[$i] - (int)$n ) % 26;
        array_push($getIndex, $decryption);
    }

    for ($i = 0; $i < count($getIndex); $i++) {
        array_push($result, $alphabet[$getIndex[$i]]);
    }

    return $result;
}

echo "Decryption" . "<br>";
var_export(caeserDecryption($encryption, $n, $alphabet));
    

?>

标签: none

添加新评论