Don Alexander Eckford's Fibonacci and Fractal Pattern Generator
CODE FOLLOWS
<?php
include('includes/header.php'); // Assuming you have a common header
// Set a maximum allowed value
$max_input = 80;
// Function to generate the Fibonacci sequence
function fibonacci($n)
{
$sequence = [0, 1]; // Starting with the first two numbers
for ($i = 2; $i < $n; $i++)
{
$sequence[] = $sequence[$i-1] + $sequence[$i-2];
}
return $sequence;
}
// Function to generate a simple fractal-like pattern
function drawFractal($level, $scaling_factor, $current = 0)
{
if ($current >= $level) return; // Base case: stop recursion
// Adjust the number of stars based on the scaling factor
$stars = min(pow(2, $current), $scaling_factor); // Ensure it doesn't exceed the scaling factor
echo str_repeat('*', $stars) . "<br>"; // Draw pattern
drawFractal($level, $scaling_factor, $current + 1); // Recursive call to draw next level
}
// Check if the form has been submitted
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$number = isset($_POST['number']) ? (int)$_POST['number'] : 0;
$screen_width = isset($_POST['screen_width']) ? (int)$_POST['screen_width'] : 0;
if ($number > $max_input)
{
echo "<p style='color: red; font-weight: bold;'>
Warning: Number too large! Please enter a value less than or equal to $max_input.
</p>";
}
elseif ($number > 0 && $screen_width > 0)
{
// Fibonacci Calculation
$fibonacci_sequence = fibonacci($number);
echo "<h2>Fibonacci Sequence up to $number terms:</h2>";
echo implode(", ", $fibonacci_sequence);
// Scale the fractal based on screen width
$scaling_factor = max(10, $screen_width / 10);
// Adjust scaling factor based on screen width
echo "<h2>Fractal Pattern with $number levels (Scaled to Screen Width):</h2>";
echo '<div class="fractal-container">';
drawFractal($number, $scaling_factor);
echo '</div>';
}
else
{
echo "<p>Please enter a valid number and ensure screen width is provided.</p>";
}
}
?>
<!-- Form to get the number from the user and screen width using JS -->
<h1>Don Alexander Eckford's Fibonacci and Fractal Pattern Generator</h1>
<form method="POST" action="">
<label for="number">Enter a number (max 80):</label>
<input type="number" id="number" name="number" min="1" max="80" required>
<input type="hidden" id="screen_width" name="screen_width">
<input type="submit" value="Generate">
</form>
<!-- JavaScript to capture screen width -->
<script>
document.getElementById('screen_width').value = window.innerWidth;
// Capture the screen width and send it in the form
</script>
<?php
include('includes/footer.php'); // Assuming you have a common footer
?>