Facebook Pixel
Using Mathematical Formulas to Control Loop Bounds

How do I find the mathematical relationship in a pattern?

The answer

Map the row number (X) to the star count (Y). Find the difference between Y values (the slope), and use basic algebra (y = mx + b) to find the formula.

More FAQs in Using Mathematical Formulas to Control Loop Bounds

The formula is (2 * row) - 1, assuming your row counter starts at 1.

The formula is exactly 2 * row, which prints 2, 4, 6, 8 stars progressively.

Using mathematical bounds inside the for-loop condition is cleaner, much faster, and scales perfectly for any value of N, unlike hardcoded if-statements.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Your learning library

Small steps. Big possibilities.

Find your next course. Build something great.

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.