Mathematical constants (pi, e, golden ratio) in SciPy - Time & Space Complexity
We want to understand how fast we can get values of famous math constants using scipy.
How does the time to get these constants change if we ask for many of them?
Analyze the time complexity of the following code snippet.
from scipy import constants
values = []
for _ in range(n):
values.append(constants.pi)
values.append(constants.e)
values.append(constants.golden)
This code collects the constants pi, e, and the golden ratio n times into a list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop running n times, each time fetching 3 constants.
- How many times: The loop runs exactly n times, repeating the constant retrieval 3 times per loop.
Each time we increase n, the number of operations grows directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 30 (10 loops x 3 constants) |
| 100 | 300 (100 loops x 3 constants) |
| 1000 | 3000 (1000 loops x 3 constants) |
Pattern observation: The operations increase evenly as n grows; doubling n doubles the work.
Time Complexity: O(n)
This means the time to get all constants grows in a straight line as we ask for more.
[X] Wrong: "Getting constants like pi or e takes a long time or grows with their value."
[OK] Correct: These constants are stored values in scipy, so fetching them is very fast and does not depend on their size or value.
Knowing how simple operations like fetching constants scale helps you understand bigger data tasks better.
"What if instead of fetching constants, we computed them with a complex formula inside the loop? How would the time complexity change?"
Practice
scipy.constants?Solution
Step 1: Recall scipy.constants common constants
Scipy provides many constants like pi, e, and the golden ratio directly accessible.Step 2: Check Avogadro's number availability
Avogadro's number is not included in scipy.constants; it is usually found in other libraries like scipy.constants.physical_constants.Final Answer:
Avogadro's number -> Option DQuick Check:
Avogadro's number not in scipy.constants [OK]
- Assuming all physical constants are in scipy.constants
- Confusing golden ratio with Avogadro's number
- Thinking Avogadro's number is a math constant
Solution
Step 1: Understand Python import syntax
To import a specific item from a module, usefrom module import item.Step 2: Apply correct import for pi
Since pi is inside scipy.constants, the correct import isfrom scipy.constants import pi.Final Answer:
from scipy.constants import pi -> Option AQuick Check:
Correct import syntax for pi [OK]
- Using 'import scipy.pi' which is invalid
- Trying 'from scipy import pi' without constants
- Incorrect import order like 'import pi from ...'
from scipy.constants import golden_ratio print(round(golden_ratio, 3))
Solution
Step 1: Identify the constant used
The code importsgolden_ratiofrom scipy.constants, which is approximately 1.6180339887.Step 2: Apply rounding to 3 decimals
Rounding 1.6180339887 to 3 decimals gives 1.618.Final Answer:
1.618 -> Option BQuick Check:
golden_ratio ≈ 1.618 [OK]
- Confusing golden ratio with pi or e
- Rounding incorrectly to 3 decimals
- Printing without importing golden_ratio
from scipy.constants import e print(e())
Solution
Step 1: Understand what e represents
In scipy.constants, e is a constant value (Euler's number), not a function.Step 2: Identify misuse of parentheses
Calling e with parenthesese()tries to call a float as a function, causing an error.Final Answer:
e is a constant, not a function; remove parentheses -> Option AQuick Check:
Constants are values, not callable functions [OK]
- Trying to call constants as functions
- Incorrect import syntax
- Thinking constants need conversion before print
Solution
Step 1: Identify the correct constant for circle area
The area of a circle formula is pi times radius squared, so we need pi.Step 2: Check code correctness
from scipy.constants import pi area = pi * 3 ** 2 print(area) imports pi correctly and calculates area as pi * 3^2, which is correct.Step 3: Verify other options
import scipy.constants as sc area = sc.pi(3) ** 2 print(area) tries to call pi as a function, which is wrong. Options C and D use wrong constants (e and golden_ratio) for area.Final Answer:
from scipy.constants import pi area = pi * 3 ** 2 print(area) -> Option CQuick Check:
Use pi * r^2 for circle area [OK]
- Calling pi as a function
- Using wrong constants for area
- Incorrect import or calculation syntax
Ready to prove it?
Pass the test and this pattern moves unknown
