简体   繁体   English

给定数字是否可以写成两个或多个连续正整数的总和?

[英]Can a given number be written as a sum of two or more consecutive positive integers?

I need to write a method which takes in an int and returns true if the number can be written as a sum of two or more consecutive positive integers and false otherwise. 我需要编写一个接受int的方法,如果数字可以写成两个或多个连续正整数的总和,则返回true否则返回false

boolean IsSumOfConsecutiveInts(int num)

I figured out that all odd numbers (except the number 1) can be written as the sum of 2 consecutive positive integers: 我发现所有奇数(除了数字1)都可以写成2个连续正整数的总和:

return (num > 1 && num % 2 == 1);

but this doesn't account for numbers that can be written as the sum of more than 2 consecutive positive integers (such as 6 == 1 + 2 + 3 ). 但是这并没有考虑到可以写成超过2个连续正整数之和的数字(例如6 == 1 + 2 + 3 )。

How can I determine whether a number can be written as a sum of two or more consecutive positive integers? 如何确定一个数字是否可以写成两个或多个连续正整数的总和?

These numbers are called Polite Numbers . 这些数字称为礼貌数字

And, conveniently, the only numbers that aren't polite are the powers of 2. 而且,方便的是,唯一不礼貌的数字 2的权力。

So, that gives us 2 options. 所以,这给了我们2个选择。 We can either determine that a number is polite , OR we can determine that it is not a power of 2 . 我们可以确定一个数字是礼貌的 ,或者我们可以确定它不是2的幂

I did both; 我做了两件事; the latter is easier (and more efficient). 后者更容易(也更有效)。

  1. This determines whether or not a number is polite : 这决定了一个数字是否有礼貌

     boolean IsSumOfConsecutiveInts(int num) { int sumOfFirstIIntegers = 3; for (int i = 2; sumOfFirstIIntegers <= num; i++) { if (i%2 == 0 ? (num%i == i/2) : (num%i == 0)) { return true; } sumOfFirstIIntegers += i + 1; } return false; } 

    This one is pretty hard to understand. 这个很难理解。 It took me a while to come up with. 我花了一段时间才提出来。

    Basically, i is the number of consecutive integers that we are checking; 基本上, i是我们正在检查的连续整数的数量;

    sumOfFirstIIntegers is equal to the sum of the first i integers, so that means that all the numbers that can be expressed as a sum of i consecutive integers are greater than or equal to sumOfFirstIIntegers . sumOfFirstIIntegers等于前i整数的总和,因此这意味着所有可以表示为i个连续整数之和的数字大于或等于sumOfFirstIIntegers

    The last part that deserves discussing is the boolean statement i%2 == 0 ? (num%i == i/2) : (num%i == 0) 值得讨论的最后一部分是布尔语句i%2 == 0 ? (num%i == i/2) : (num%i == 0) i%2 == 0 ? (num%i == i/2) : (num%i == 0) . i%2 == 0 ? (num%i == i/2) : (num%i == 0) Let's look at some examples: 我们来看一些例子:

     i all sums of i consecutive positive integers 2 3, 5, 7, 9... 3 6, 9, 12, 15... 4 10, 14, 18, 22... 5 15, 20, 25, 30... 

    There are two cases, but in either case, we can express all possible numbers that are a sum of i consecutive integers pretty simply. 有两种情况,但在任何一种情况下,我们都可以非常简单地表达所有可能的数字,它们是i个连续整数的总和。

    1. When i is even, num must be equal to (i * n) + (i / 2) where n is a non-negative integer. i是偶数时, num必须等于(i * n) + (i / 2) ,其中n是非负整数。 This can of course be written as num % i == i / 2 . 这当然可以写成num % i == i / 2

    2. When i is odd, num must be equal to i * n , where n is a non-negative integer. i为奇数时, num必须等于i * n ,其中n是非负整数。 Which gives us our second condition num % i == 0 . 这给了我们第二个条件num % i == 0

    In addition to these conditions, num can not be less than the sum of the first i positive integers. 除了这些条件之外, num不能小于前i正整数的总和。 Hence, our for loop's conditional: sumOfFirstIIntegers <= num . 因此,我们的for循环有条件: sumOfFirstIIntegers <= num

  2. This determines whether a number is not a power of 2 : 这确定一个数字是否不是2的幂

     boolean IsSumOfConsecutiveInts(int num) { return (num & (num - 1)) != 0; } 

    This answer does a good job of explaining why this works. 这个答案很好地解释了为什么这有效。

Note that both of the above solutions have the same result, they are just different ways of thinking about the problem. 请注意,上述两种解决方案都具有相同的结果,它们只是思考问题的不同方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM