{"id":9992,"date":"2023-04-27T21:23:08","date_gmt":"2023-04-28T04:23:08","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9992"},"modified":"2023-04-27T21:29:07","modified_gmt":"2023-04-28T04:29:07","slug":"leetcode-2652-sum-multiples","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-2652-sum-multiples\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2652. Sum Multiples"},"content":{"rendered":"\n<p>Given a positive integer&nbsp;<code>n<\/code>, find the sum of all integers in the range&nbsp;<code>[1, n]<\/code>&nbsp;<strong>inclusive<\/strong>&nbsp;that are divisible by&nbsp;<code>3<\/code>,&nbsp;<code>5<\/code>, or&nbsp;<code>7<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>an integer denoting the sum of all numbers in the given range satisfying&nbsp;the constraint.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 7\n<strong>Output:<\/strong> 21\n<strong>Explanation:<\/strong> Numbers in the range <code>[1, 7]<\/code> that are divisible by <code>3<\/code>, <code>5,<\/code> or <code>7 <\/code>are <code>3, 5, 6, 7<\/code>. The sum of these numbers is <code>21<\/code>.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 10\n<strong>Output:<\/strong> 40\n<strong>Explanation:<\/strong> Numbers in the range <code>[1, 10] that are<\/code> divisible by <code>3<\/code>, <code>5,<\/code> or <code>7<\/code> are <code>3, 5, 6, 7, 9, 10<\/code>. The sum of these numbers is 40.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 9\n<strong>Output:<\/strong> 30\n<strong>Explanation:<\/strong> Numbers in the range <code>[1, 9]<\/code> that are divisible by <code>3<\/code>, <code>5<\/code>, or <code>7<\/code> are <code>3, 5, 6, 7, 9<\/code>. The sum of these numbers is <code>30<\/code>.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 10<sup>3<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Mod<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int sumOfMultiples(int n) {\n    int ans = 0;\n    for (int i = 1; i <= n; ++i) {\n      if (i % 3 == 0 || i % 5 == 0 || i % 7 == 0)\n        ans += i;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a positive integer&nbsp;n, find the sum of all integers in the range&nbsp;[1, n]&nbsp;inclusive&nbsp;that are divisible by&nbsp;3,&nbsp;5, or&nbsp;7. Return&nbsp;an integer denoting the sum of all&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[222,31,158],"class_list":["post-9992","post","type-post","status-publish","format-standard","hentry","category-math","tag-easy","tag-math","tag-mod","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9992","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9992"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9992\/revisions"}],"predecessor-version":[{"id":9994,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9992\/revisions\/9994"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9992"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9992"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9992"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}