Created
May 29, 2018 12:44
-
-
Save nairihar/aa852f7322f8dcbd8df1fb184e227f13 to your computer and use it in GitHub Desktop.
Optym Internship, lesson 3, game description and solutions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Find the number of ways to express n as sum of some (at least two) consecutive positive integers. | |
Example | |
For n = 9, the output should be | |
isSumOfConsecutive2(n) = 2. | |
There are two ways to represent n = 9: 2 + 3 + 4 = 9 and 4 + 5 = 9. | |
For n = 8, the output should be | |
isSumOfConsecutive2(n) = 0. | |
There are no ways to represent n = 8 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isSumOfConsecutive2(t) { | |
let sum = 0; | |
for(let i = 2; i < t; i++) | |
{ | |
let a = 0; | |
let temp = 2*t + i*(1-i); | |
if(temp%(2*i) != 0) continue; | |
a = temp/(2*i); | |
if(a > 0 && a < t) { | |
sum++; | |
} | |
} | |
return sum; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isSumOfConsecutive2(n) { | |
var c = 0; | |
var s = 0; | |
for (var i=1; i<n; i++) { | |
for(var j=i; j<n; j++) { | |
s+= j; | |
if (s==n) { | |
c++; | |
break; | |
} | |
if (s>n) { | |
break; | |
} | |
} | |
s=0 | |
} | |
return c | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment