## Longest Sequence Of Consecutive Odd Integers

### October 20, 2015

Today’s exercise is to find the longest sequence of consecutive odd integers that add to a given sum. For instance, given the target 160701, the three consecutive odd integers 53565, 53567 and 53569 sum to 160701, but the 391 consecutive odd integers from 21 to 801 also sum to 160701, and are the longest sequence of consecutive odd integers to do so, so they are the correct answer.

Your task is to write a program to find the longest sequence of consecutive odd integers that add to a given sum. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

### 6 Responses to “Longest Sequence Of Consecutive Odd Integers”

1. Francesco said

Not the haskellest/most efficient implementation but…

```g n = f n [] (enumFromThenTo 1 3 n) []
where f n e []     r = r
f n e (a:as) r | sum e <  n          = f n (e ++ [a]) as     r
| sum e >  n          = f n (tail e)   (a:as) r
| length e > length r = f n (tail e)   (a:as) e
| otherwise           = f n (tail e)   (a:as) r
```
2. Paul said

Two solutions in Python. The functions powerset is from the itertools documentation, is_prime and rho_factors are from the Primes Essay (ProgrammingPraxis). isqrt is the integer square root.

```from operator import mul
from functools import reduce

NO_SOLUTION = (0,0,0)

def solve(n):
if n % 4 == 2:
return NO_SOLUTION
lo, hi, s = 1, 1, 1
it = 0
while lo <= n:
if s < n:
hi += 2
s += hi
elif s > n:
s -= lo
lo += 2
else:
break
else:
return NO_SOLUTION
nrterms = (hi - lo) // 2 + 1
return lo, hi, nrterms

def solve2(n):
""" The number of terms is equal to the highest divisor D of n, that
is lower than the square root of n.
The midpoint of the range is n // D
If n is even both the number of terms and the midpoint should be even.
If n is even: remove  a factor 4 and multiply the highest divisor by 2,
to make sure that the number of terms and the midpoint are even.
There is no solution if n = 2  mod 4 (as there is only one factor 2)
"""
if n % 4 == 2:
return NO_SOLUTION
if is_prime(n):
return n, n, 1
m, fac = (n // 4, 2) if n % 2 == 0 else (n, 1)
divisors = set(reduce(mul, arr, 1) for arr in powerset(rho_factors(m)))
divisors = sorted((d for d in divisors if d <= isqrt(m)), reverse=True)
nrterms = divisors * fac
mid = n // nrterms
return mid - nrterms + 1, mid + nrterms - 1, nrterms
```
3. Mike said

The sum of the first k odd numbers is k*k. Therefore, the sum of consecutive odd numbers is the difference between two perfect squares. For example, 160701 = 160801 – 100 = 401*401 – 10*10 = sum from 11th odd number to the 401st odd number (note: the sum of the first 10 odd numbers are subtracted, so the 11th odd number is the first one in the sequence of consecutive odd numbers).

If the search starts from zero, then the first sequence found that sums to n is necessarily the longest, because adding another term at the high end will require subtracting at least one term from the small end.

```from math import sqrt

def findsumofodd(n):
wanted = set()
k, k2 = 0, 0
limit = n//2 + 1
while k <= limit:
if k2 in wanted:
j = int(sqrt(k2 - n)) + 1
return 2*j - 1, 2*k - 1

k2 += 2*k + 1
k += 1
```

N.B.: 53569 is the 26785th odd number and 53565 is the 26783rd odd number.

26785**2 – (26783 – 1)**2 = 717436225 – 717275524 = 160701

4. matthew said

A couple more Python solutions, the first moves the starting position b up from 0, maintaining end position a with sum s such that s >= n (so it’s quite like Paul’s first solution). The second is based on Mike’s insight that we are after a factorization of n of the form (a+b)(a-b) and is basically Fermat’s factorization algorithm (it could be improved with a better check for t being an exact square):

```def isqrt(n):
if n == 0: return 0
x = n
while True:
y = (x + n//x)//2
if (y >= x): return x
x = y

def solve0(n):
if n%4 == 2: return None
a,b = isqrt(n), 0
if a*a < n: a += 1
s = a*a # sequence sum: n <= s
while True:
if s == n: return (b+1,a)
s -= 2*b+1
b += 1
while s < n:
s += 2*a+1;
a += 1

def solve1(n):
if n%4 == 2: return None
a = isqrt(n)
if a*a < n: a += 1
while True:
t = a*a - n
b = isqrt(t)
if b*b == t: return (b+1,a)
a += 1
```
5. fisherro said

A quick & dirty version in C++. (Although, except for iostreams, it is a C version as well.)

```#include <iostream>

//Longest sequence of consecutive odd integers
void lsqcoi(int target)
{
int longest_start = 0;
int longest_end = 0;
int longest_count = 0;
for (int start = 1; start <= target; start += 2) {
//We could bail if longest_count > ((target - start) / 2).
int total = start;
int count = 1;
int next = start;
while (true) {
if (total == target) {
if (count > longest_count) {
longest_start = start;
longest_end = next;
longest_count = count;
}
}
if (total >= target) break;
next += 2;
++count;
total += next;
}
}
if (0 == longest_count) {
std::cout << "Found no sequence of odd integers that sum to " <<
target << ".\n";
} else if (longest_start == longest_end) {
std::cout << "The longest count of odd integers that sum to " <<
target << " is itself!\n";
} else {
std::cout << "The " << longest_count << " odd integers from " <<
longest_start << " to " << longest_end << " is the longest "
"sequence of consecutive odd integers that sum to " << target <<
".\n";
}
}

int main()
{
lsqcoi(1);
lsqcoi(2);
lsqcoi(4);
lsqcoi(160701);
}
```
6. fl84sg said

a production of my pencil and keyboard, supported by a bit of mathematics

```#include <stdio.h>
#include <math.h>

void longest_streak(int num) { //a function which prints out the interval of solution
int start, count;
count = sqrt(num); //the largest count of numbers to be summed is less than square root of num

while(count > 0 && (num % (--count + 1) != 0 || (num / (count + 1) - count) % 2 == 0) );
//count can't be less than 1 the loop continues unless a divisor of num is found which results into an odd starting number

start = num / (count + 1) - count;
printf("%d equates sum of odd integers from %d to %d\n", num, start, start + 2 * count);
}

void main() {
longest_streak(2); //it is obvious that 2 and any other prime number cannot be written as sum of odd integers
longest_streak(6); //six tends to be an exception and i don't know why, if you know just let me know
longest_streak(160701);
}
```