From 1a0a0c1ebfe6683aea546d14e7715859128cf08c Mon Sep 17 00:00:00 2001
From: KAPIAS NICOLAS <nicolas.kapias@etu.unistra.fr>
Date: Thu, 13 Mar 2025 10:26:02 +0100
Subject: [PATCH] exercice 3 04/02/2025

---
 fibonacci.php     |  62 ++++++++++++++++
 prime.php         |  36 +++++++++
 prime_hundred.php |  28 +++++++
 rand_array.php    | 183 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 309 insertions(+)
 create mode 100755 fibonacci.php
 create mode 100755 prime.php
 create mode 100755 prime_hundred.php
 create mode 100755 rand_array.php

diff --git a/fibonacci.php b/fibonacci.php
new file mode 100755
index 0000000..f3ca578
--- /dev/null
+++ b/fibonacci.php
@@ -0,0 +1,62 @@
+<html>
+	<head>
+		<title>M2GPI</title>
+	</head>
+	<body>
+		<form method="POST" action="fibonacci.php">
+			<input type="submit" name="calcul" value="Calculer"/>
+			le fibonacci de <input type="number" name="input"/> ?
+		</form>
+		<?php
+			function iterativeFibonacci($count) {
+				$fibonacci = array(0,1);
+				for ($index=2; $index <= $count; $index++) { 
+					$fibonacci[$index] = $fibonacci[$index-1] + $fibonacci[$index-2];
+				}
+				return $fibonacci[$count];
+			}
+
+			function recursiveFibonacci($count) {
+				if ($count == 0) {
+					return 0;
+				} elseif ($count == 1) {
+					return 1;
+				} else {
+					return (recursiveFibonacci($count - 1) + recursiveFibonacci($count - 2));
+				}
+			}
+			if (isset($_POST['calcul']) && $_POST['input']) {
+
+				echo "<h3>Fonction itérative</h3>";
+				$start_time = microtime(true);
+				echo "<p>L'occurence n°" . $_POST['input'] . " de la suite de fibonacci est " . iterativeFibonacci($_POST['input']) . "</p>";
+				echo "<p>Le temps d'exécution est de " . number_format((microtime(true) - $start_time), 6) . " seconde(s)";
+
+				echo "<h3>Fonction récursive</h3>";
+				$start_time = microtime(true);
+				echo "<p>L'occurence n°" . $_POST['input'] . " de la suite de fibonacci est " . recursiveFibonacci($_POST['input']) . "</p>";
+				echo "<p>Le temps d'exécution est de " . number_format((microtime(true) - $start_time), 6) . " seconde(s)";
+
+			} else {
+				echo "<p>Veuillez fournir un nombre à évaluer.</p>";
+			}
+		?>
+		<table>
+			<tr>
+				<th>Fibonacci</th>
+				<th>Itératif</th>
+				<th>Récursif</th>
+			</tr>
+			<?php
+				for ($i=0; $i < 35; $i++) {
+					$start_time = microtime(true);
+					echo "<tr><td>" . iterativeFibonacci($i) . "</td>";
+					echo "<td>" . number_format((microtime(true) - $start_time), 6) . "s</td>";
+					$start_time = microtime(true);
+					recursiveFibonacci($i);
+					echo "<td>" . number_format((microtime(true) - $start_time), 6) . "s</td></tr>";
+				}
+			?>
+		</table>
+	</body>
+</html>
diff --git a/prime.php b/prime.php
new file mode 100755
index 0000000..15eeeeb
--- /dev/null
+++ b/prime.php
@@ -0,0 +1,36 @@
+<html>
+	<head>
+		<title>M2GPI</title>
+	</head>
+	<body>
+		<form method="POST" action="prime.php">
+			<input type="submit" name="calcul" value="Calculer"/>
+			si <input type="number" name="input"/> est un nombre premier ?
+		</form>
+		<?php
+
+
+			function isPrime($n) {
+				for ($i=2; $i < $n ; $i++) { 
+					if ($n % $i == 0) {
+						return false;
+					}
+				}
+				return true;
+			}
+
+
+			if (isset($_POST['calcul']) && $_POST['input']) {
+				if ($_POST['input'] > 2 && isPrime($_POST['input'])) {
+					echo $_POST['input'] . " est un nombre premier.";
+				} else {
+					echo $_POST['input'] . " n'est pas un nombre premier.";
+				}
+			} else {
+				echo "Veuillez fournir un nombre à évaluer.";
+			}
+
+			
+		?>
+	</body>
+</html>
diff --git a/prime_hundred.php b/prime_hundred.php
new file mode 100755
index 0000000..c930da1
--- /dev/null
+++ b/prime_hundred.php
@@ -0,0 +1,28 @@
+<html>
+	<head>
+		<title>M2GPI</title>
+	</head>
+	<body>
+		<?php
+			function isPrime($n) {
+				for ($i=2; $i < $n ; $i++) { 
+					if ($n % $i == 0) {
+						return false;
+					}
+				}
+				return true;
+			}
+
+			$primes = array();
+			for ($i=2; count($primes) < 100; $i++) { 
+				if (isPrime($i)) {
+					$primes[] = $i;
+				}
+			}
+
+			foreach ($primes as $key => $value) {
+				echo $key . " : " . $value . "<br>";
+			}
+		?>
+	</body>
+</html>
diff --git a/rand_array.php b/rand_array.php
new file mode 100755
index 0000000..846ec74
--- /dev/null
+++ b/rand_array.php
@@ -0,0 +1,183 @@
+<html>
+	<!-- 
+		Calcul sur tableau
+		1 - générer un tableau avec N nombres aléatoires
+		2 - calculer la moyenne, le plus petit et le plus grand
+		3 - trier les valeurs par tri à bulle et les afficher
+		4 - rechercher une valeur dans la table par dichotomie
+	-->
+	<head>
+		<title>M2GPI</title>
+		<style>
+			body {
+				display: flex;
+				flex-direction: column;
+			}
+			#forms {
+				margin: auto;
+				display: flex;
+				align-items: flex-end;
+				align-content: space-between;
+			}
+			#results {
+				margin: auto;
+			}
+			#tables {
+				margin: auto;
+				display: flex;
+				align-content: space-between;
+			}
+			#forms > form {
+				margin: 0 1em 0 1em;
+			}
+			#tables > div {
+				margin: 0 5em 0 5em;;
+			}
+		</style>
+	</head>
+	<body>
+		<?php
+			function displayGenerateForm() : string {
+				$string = 	
+					'<form method="POST" action="rand_array.php">
+						<input type="number" name="size" placeholder="number of entries"/><br>
+						<input type="number" name="min" placeholder="minimum values"/><br>
+						<input type="number" name="max" placeholder="maximal values"/><br>
+						<input type="submit" name="generate" value="Generate array"/>
+					</form>';
+				return $string;
+			}
+
+			function displaySearchForm($array) : string {
+				$string = 	
+					'<form method="POST" action="rand_array.php">
+					<input type="number" name="input" placeholder="value to find"/><br>
+					<input type="submit" name="search" value="Search in array"/>';
+				foreach($array as $value) {
+					$string .= '<input type="hidden" name="array[]" value="' . $value . '">';
+				}
+				$string .= '</form>';
+				return $string;
+			}
+
+			function displayArray($array) : string {
+				$string = "<table><tr><th>Index</th><th>Value</th></tr>";
+				$count = count($array);
+				for ($i=0; $i < $count; $i++) { 
+					$string .= "<tr><td>" . $i . "</td>";
+					$string .= "<td>" . $array[$i] . "</td></tr>";
+				}
+				$string .= "</table>";
+				return $string;
+			}
+
+			function generateRandArray($size = 20, $min = 1, $max = 1000) : array {
+				$array = array();
+				for ($i=0; $i < $size; $i++) { 
+					$array[] = rand($min, $max);
+				}
+				return $array;
+			}
+
+			function calculateAverage($array) : int {
+				$int = (int)0;
+				$count = count($array);
+				for ($i=0; $i < $count; $i++) { 
+					$int = $int + $array[$i];
+				}
+				$int = $int / count($array);
+				return $int;
+			}
+
+			function calculateMinimum($array) : int {
+				$int = $array[0];
+				$count = count($array);
+				for ($i=0; $i < $count; $i++) { 
+					if ($array[$i] < $int) {
+						$int = $array[$i];
+					}
+				}
+				return $int;
+			}
+
+			function calculateMaximum($array) : int {
+				$int = $array[0];
+				$count = count($array);
+				for ($i=0; $i < $count; $i++) { 
+					if ($array[$i] > $int) {
+						$int = $array[$i];
+					}
+				}
+				return $int;
+			}
+
+			function bubbleSort($array) : array {
+				$temp = (int)0;
+				$count = count($array);
+				$swapped = (bool)true;
+				while ($swapped) {
+					$swapped = false;
+					$count = $count - 1;
+					for ($i=0; $i < $count; $i++) { 
+						if ($array[$i] > $array[$i + 1]) {
+							$swapped = true;
+							$temp = $array[$i + 1];
+							$array[$i + 1] = $array[$i];
+							$array[$i] = $temp;
+						}
+					}
+				}
+				return $array;
+			}
+
+			function searchValue($value, $array) : int {
+				$min = (int)0;
+				$max = count($array) - 1;
+				while ($min <= $max) {
+					$index = floor(($min + $max) / 2);
+					if ($array[$index] == $value) {
+						return $index;
+					} elseif ($array[$index] < $value) {
+						$min = $index + 1;
+					} elseif ($array[$index] > $value) {
+						$max = $index - 1;
+					}
+				}
+				return -1;
+			}
+
+			if (isset($_POST['search']) && $_POST['input'] && $_POST['array']) {
+				$input = $_POST['input'];
+				$array = $_POST['array'];
+			} elseif (isset($_POST['generate']) && $_POST['size'] && $_POST['min'] && $_POST['max']) {
+				$array = generateRandArray($_POST['size'], $_POST['min'], $_POST['max']);
+			} else {
+				$array = generateRandArray();
+			}
+			$sortedArray = bubbleSort($array);
+			echo '<h1>M2GPI2024 Algorithmie</h1>';
+			echo '<div id="forms">';
+			echo displayGenerateForm();
+			echo displaySearchForm($array);
+			echo '</div><div id="results">';
+			echo '<h3>Results</h3>';
+			echo "<p>Average value is " . calculateAverage($array) . "</p>";
+			echo "<p>Smallest value is " . calculateMinimum($array) . "</p>";
+			echo "<p>Largest value is " . calculateMaximum($array) . "</p>";
+			if (isset($input)) {
+				$result = searchValue($input, $sortedArray);
+				if ($result > -1) {
+					echo "Value " . $input . " exists at index " . $result;
+				} else {
+					echo "Value " . $input . " doesn't exist";
+				}
+			}
+			echo '</div><div id="tables">';
+			echo '<div><h3>Unordered</h3>';
+			echo displayArray($array);
+			echo '</div><div><h3>Ordered</h3>';
+			echo displayArray($sortedArray);
+			echo '</div></div>';
+		?>
+	</body>
+</html>
\ No newline at end of file
-- 
GitLab