FlareStart
HomeNewsHow ToSources
FlareStart

Where developers start their day. All the tech news & tutorials that matter, in one place.

Quick Links

  • Home
  • News
  • Tutorials
  • Sources
  • Privacy Policy

Connect

© 2026 FlareStart. All rights reserved.

Back to articles
Understanding Fast Power (Exponentiation by Squaring) - Pow(x, n) Step-by-Step - LeetCode 50
How-ToMachine Learning

Understanding Fast Power (Exponentiation by Squaring) - Pow(x, n) Step-by-Step - LeetCode 50

via Dev.toPurav Patel1mo ago

With Clear Diagrams + Recursive & Iterative Methods (C#) When solving Pow(x, n) in interviews (Google, Meta, etc.), a brute-force solution is NOT enough. The naive way: x * x * x * x * ... (n times) This takes O(n) time. But we can do better. We can solve it in: O(log n) Let's understand this deeply --- visually and line-by-line. 🧠 The Key Mathematical Insight If n is EVEN: x^n = (x^(n/2))² Example: 2^8 = (2^4)² = (16)² = 256 Why? Because: 2^8 = 2×2×2×2×2×2×2×2 = (2×2×2×2) × (2×2×2×2) = 2^4 × 2^4 If n is ODD: x^n = (x^((n-1)/2))² × x Example: 2^9 = 2 × 2^8 = 2 × (2^4)² We remove one x to make the exponent even. 🚀 Why This Is Faster Instead of reducing: n → n-1 → n-2 → ... We reduce: n → n/2 → n/4 → n/8 → ... So recursion depth becomes: O(log n) 📘 Recursive Method (C#) public class Solution { public double MyPow ( double x , int n ) { long N = n ; if ( N < 0 ) { x = 1 / x ; N = - N ; } return FastPow ( x , N ); } private double FastPow ( double x , long n ) { if ( n == 0 ) return 1 ; do

Continue reading on Dev.to

Opens in a new tab

Read Full Article
46 views

Related Articles

Red Rooms makes online poker as thrilling as its serial killer
How-To

Red Rooms makes online poker as thrilling as its serial killer

The Verge • 1d ago

Don’t Know What Project to Build? Here Are Developer Projects That Actually Make You Better
How-To

Don’t Know What Project to Build? Here Are Developer Projects That Actually Make You Better

Medium Programming • 2d ago

Why Most Developers
Stay Broke
How-To

Why Most Developers Stay Broke

Medium Programming • 2d ago

Building a Simple Lab Result Agent in .NET (Microsoft Agent Framework + Ollama)
How-To

Building a Simple Lab Result Agent in .NET (Microsoft Agent Framework + Ollama)

Medium Programming • 2d ago

“You don’t need to learn programming anymore” — Reality Check from a CTO
How-To

“You don’t need to learn programming anymore” — Reality Check from a CTO

Medium Programming • 2d ago

Discover More Articles