Beginner Concepts: Introduction to Loops in SageMath
- Get link
- X
- Other Apps
Loops are the building blocks for creating smart, efficient, and dynamic solutions.
In SageMath, mastering loops unlocks the door to exciting mathematical modeling
and automation!
Let's dive in!
What Are Loops in Programming?
Loops repeat a block of code automatically, saving
time and avoiding repetition.
In SageMath, they’re essential for calculations, simulations, and plotting.
Why Are Loops Useful in SageMath?
Loops allow you to:
Basic Concepts of Loops in SageMath
For Loops
Use a for loop when the number of repetitions is
known.
Syntax:
Example: Printing Numbers 1 to 5
Example:
Printing the amount received on an investment under compound interest every year for 10 year.
Suppose we want to print the amount received on an
investment under compound interest until
the returned is doubled.
Use of break
Example:
Generate a random list of 20 integers one by one between 1 and 100 and find their sum.
Example:
Generate a random number
between 0 and 1. If the number generated is >= 0.5, then assign it to ‘head’
else assign it to ‘tail’. Do this experiment 500 times and count number of
heads and tail.
Example:
Find the sum of integers between 1 and 100 (both inclusive) which are divisible 2 or divisible by 3 or divisible by 5. Also count how many such integers are there.
Use of continue
Example:
How many integers are there
between 1 and 100 (both inclusive) which are not divisible by 2, 3 and 5? Also
print these numbers.
π For Loop Flowchart
While Loops
Use a while loop when repetition depends on a
condition.
Syntax:
Example:
Performing addition until the total becomes greater than 10.
Example:
In
how many years an investment of Rs. 20000 will double if interest is calculated
under compound interest at the rate 5% annually?
The formula for compound
amount is:
You want to find the smallest
n such that:
Example:
Use
while loop to find the gcd of two integers a and b.
Example:
How many tosses are rquired to get sixes on both the faces if two six faced dice are tossed together?
Use of break and continue with while loop
Example:
Write a python code to find the sum of positive integers starting with 1 which are not divisible by 2 and 3. Stop when the sum exceeds 1000.
Use of else with loops
Python supports use of else with loops.
If the else is used with a for loop, then it is executed when the for loop finishes iterating.
If the else is used with a while loop, then it is executed when the condition becomes
false.
Example:
Check
if there is a prime number between two postive integers a and
b.
π While Loop Flowchart
Practical Applications of Loops in SageMath
1. Approximating Square Roots (Babylonian Method)
2. Plotting Multiple Functions
3. Filtering Data with Conditional Loops
Tips and Tricks for Beginners
✅ Smart use of range() —
remember it excludes the end value.
✅ Indent carefully —
Python and SageMath are indentation-sensitive!
✅ Start small — testing on
small inputs saves debugging headaches.
✅ Troubleshooting Tip:
- If
SageMath isn’t working, check your installation or try SageMathCell for
easy browser execution.
- Watch
for missing imports like import random in simulation tasks.
Your Turn! π
π¬ Challenges to Try:
π― Bonus Challenge: Simulating Random Data + Advanced Loops
Teaser for Next Time π₯
You’ve now mastered the basics of loops!
Coming up next:
We'll use loops to create animations — imagine simulating real-world models like:Get ready to bring your math to life with SageMath!
- Get link
- X
- Other Apps
Comments
Post a Comment
If you have any queries, do not hesitate to reach out.
Unsure about something? Ask away—I’m here for you!