Program List 8 Ada
Program List 8 Ada
1) Implementation and time analysis of factorial program using iterative and recursive method.
Program:
import time
def factorial_iterative(n):
result = 1
for i in range(1, n + 1):
result *= i
return result
def factorial_recursive(n):
if n == 0:
return 1
else:
return n * factorial_recursive(n - 1)
def measure_time(func, *args):
start_time = time.time()
result = func(*args)
end_time = time.time()
execution_time = end_time - start_time
return result, execution_time
input_values = [5, 10]
for n in input_values:
print(f"Factorial of {n} (Iterative):")
result_iterative, time_iterative = measure_time(factorial_iterative, n)
print(f"Result: {result_iterative}, Execution Time: {time_iterative:.6f} seconds\n")
print(f"Factorial of {n} (Recursive):")
result_recursive, time_recursive = measure_time(factorial_recursive, n)
print(f"Result: {result_recursive}, Execution Time: {time_recursive:.6f} seconds\n")
print("-" * 50)
Output:
Factorial of 5 (Iterative):
Result: 120, Execution Time: 0.000001 seconds
Factorial of 5 (Recursive):
Result: 120, Execution Time: 0.000003 seconds
Question 1: A practical use for recursion in game development (At least 5 usage).
1. World Generation:
Recursive algorithms can be used to generate complex and natural-looking game worlds. For
example, the diamond-square algorithm.
2. Fractal Generation:
Recursion is used to create fractal patterns and structures, which can be seen in game art,
level design, or even procedural textures.
3. Maze Generation:
Recursive algorithms like the recursive backtracking or divide-and-conquer can be used to
generate random mazes for games.
4. Tree Structures:
Game development often involves hierarchical structures, such as scene graphs, object
hierarchies, or decision trees for AI.
5. Pathfinding:
Recursive algorithms like A* and Dijkstra's algorithm can be used for pathfinding in games.
These algorithms involve exploring possible paths through a graph, which can be implemented
using recursion to backtrack and explore alternative routes until the optimal path is found.