<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: sanya pandey</title>
    <description>The latest articles on DEV Community by sanya pandey (@sanya_pandey_92410fb7c26a).</description>
    <link>https://dev.to/sanya_pandey_92410fb7c26a</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4027140%2F7fd08d04-e993-4f26-ae7f-93b7ff9f74bd.png</url>
      <title>DEV Community: sanya pandey</title>
      <link>https://dev.to/sanya_pandey_92410fb7c26a</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sanya_pandey_92410fb7c26a"/>
    <language>en</language>
    <item>
      <title>The Multiples of 3 and 5: How One Tiny Math Puzzle Teaches You to Think Like an Engineer</title>
      <dc:creator>sanya pandey</dc:creator>
      <pubDate>Mon, 13 Jul 2026 11:01:00 +0000</pubDate>
      <link>https://dev.to/sanya_pandey_92410fb7c26a/the-multiples-of-3-and-5-how-one-tiny-math-puzzle-teaches-you-to-think-like-an-engineer-k32</link>
      <guid>https://dev.to/sanya_pandey_92410fb7c26a/the-multiples-of-3-and-5-how-one-tiny-math-puzzle-teaches-you-to-think-like-an-engineer-k32</guid>
      <description>&lt;p&gt;Project Euler is a site with 900+ problems that mix math and code. Problem 1 is the classic starting point — small enough to brute-force in seconds, but rich enough to teach a lesson every engineer needs: check if there's a formula before you write a loop.&lt;/p&gt;

&lt;p&gt;The Problem&lt;/p&gt;

&lt;p&gt;Add up every number below 1000 that is a multiple of 3 or 5.&lt;/p&gt;

&lt;p&gt;A multiple of 3 is any number you get by multiplying 3 by a whole number (3, 6, 9, 12...). Divisibility just means the division has no remainder — in Python, number % 3 == 0 checks exactly that.&lt;/p&gt;

&lt;p&gt;Brute Force Solution&lt;/p&gt;

&lt;p&gt;pythondef sum_of_multiples(limit):&lt;br&gt;
    total = 0&lt;br&gt;
    for number in range(1, limit):&lt;br&gt;
        if number % 3 == 0 or number % 5 == 0:&lt;br&gt;
            total += number&lt;br&gt;
    return total&lt;/p&gt;

&lt;p&gt;print(sum_of_multiples(1000))&lt;/p&gt;

&lt;p&gt;This loops through every number, checks divisibility with %, and adds qualifying numbers to total. Simple — but it's O(n): double the limit, double the work. For 1000 that's fine. For a billion, it's a billion unnecessary checks.&lt;/p&gt;

&lt;p&gt;The Smarter Way: Use a Formula&lt;/p&gt;

&lt;p&gt;Multiples of 3 form an arithmetic progression: 3, 6, 9, 12... Gauss showed that the sum of the first n whole numbers is:&lt;/p&gt;

&lt;p&gt;Sum = n(n + 1) / 2&lt;/p&gt;

&lt;p&gt;So the sum of multiples of 3 is just 3 × [n(n+1)/2], where n is how many multiples exist below the limit.&lt;/p&gt;

&lt;p&gt;The catch: 15 is a multiple of both 3 and 5. If you add sum(multiples of 3) + sum(multiples of 5), you count 15, 30, 45... twice. This is the inclusion-exclusion principle — fix it by subtracting the overlap:&lt;/p&gt;

&lt;p&gt;Answer = sum(3) + sum(5) - sum(15)&lt;/p&gt;

&lt;p&gt;Optimized Code&lt;/p&gt;

&lt;p&gt;pythondef sum_of_multiples(x, limit):&lt;br&gt;
    n = (limit - 1) // x&lt;br&gt;
    return x * n * (n + 1) // 2&lt;/p&gt;

&lt;p&gt;def project_euler_1(limit=1000):&lt;br&gt;
    return (&lt;br&gt;
        sum_of_multiples(3, limit)&lt;br&gt;
        + sum_of_multiples(5, limit)&lt;br&gt;
        - sum_of_multiples(15, limit)&lt;br&gt;
    )&lt;/p&gt;

&lt;p&gt;print(project_euler_1(1000))&lt;/p&gt;

&lt;p&gt;// is integer division — it rounds down, giving whole-number counts, which is exactly what we need when counting multiples.&lt;/p&gt;

&lt;p&gt;Why It Matters: O(n) vs O(1)&lt;/p&gt;

&lt;p&gt;LimitBrute force opsFormula ops1,000~1,000~101,000,000~1,000,000~101,000,000,000~1,000,000,000~10&lt;/p&gt;

&lt;p&gt;The loop's cost grows with input size (O(n)). The formula's cost stays flat (O(1)) — it never "counts," it just calculates. This same idea shows up in database indexing, crypto (modular arithmetic), vectorized ML operations, and billing systems that can't afford to iterate over every transaction.&lt;/p&gt;

&lt;p&gt;Takeaways&lt;/p&gt;

&lt;p&gt;Pattern recognition beats brute force once you see the structure.&lt;br&gt;
Arithmetic series + inclusion-exclusion turn a loop into a formula.&lt;br&gt;
Reusable functions (sum_of_multiples) keep code DRY.&lt;br&gt;
Always ask: "Do I need to check every case, or can I calculate the answer?"&lt;/p&gt;

&lt;p&gt;That question is worth more than any single algorithm — it's the mindset behind fast, scalable software.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fk33i52llfqi1y7yz9dn3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fk33i52llfqi1y7yz9dn3.png" alt=" " width="800" height="336"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>webdev</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
