Longest Sequence Of Consecutive Odd Integers
October 20, 2015
The longest sequence is the one with the smallest starting point, so we will start from 1 and work our way up using the following algorithm:
(define (lscsi1 target) (let loop ((lo 1) (hi 1) (sum 1)) ; (for-each display `(,lo " " ,hi " " ,sum #\newline)) (cond ((< target hi) (values 0 0 0)) ((< sum target) (loop lo (+ hi 2) (+ sum hi 2))) ((< target sum) (loop (+ lo 2) hi (- sum lo))) (else (values lo hi (+ (/ (- hi lo) 2) 1))))))
>pre>> (lscsi1 160701)
21
801
391
If the current sum is too low, we increase hi
. If the current sum is too high, we increase lo
. The sum quickly increases to a value slightly larger than the target, then lo
and hi
dance until the target is reached.
An alternative given at http://stackoverflow.com/a/33113763/448810 uses some math. The sum of a sequence of n odd integers with average (midpoint) m is n × m, and the first and last numbers in the sequence are m − n + 1 and m + n − 1; further, both n and m must have the same even/odd parity. If you start at 1, the sum of the sequence is m × n = (first + n − 1) × n = n × n, so the longest sequence for any target is the square root of the target. Thus, we search for n starting from the square root and working downward until m is an integer and the same parity as n:
(define (lscsi2 target) (let loop ((n (isqrt target))) (let ((m (quotient target n))) (cond ((positive? (modulo target n)) (loop (- n 1))) ((= (modulo n 2) (modulo m 2)) (values (- m n -1) (+ m n -1) n)) (else (loop (- n 1)))))))
> (lscsi2 160701) 21 801 391
We used isqrt
from the Standard Prelude. You can run the program at http://ideone.com/d7nDRV, which also shows a good example of the dance of lscis1
.
Not the haskellest/most efficient implementation but…
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.
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.
N.B.: 53569 is the 26785th odd number and 53565 is the 26783rd odd number.
26785**2 – (26783 – 1)**2 = 717436225 – 717275524 = 160701
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):
A quick & dirty version in C++. (Although, except for iostreams, it is a C version as well.)
a production of my pencil and keyboard, supported by a bit of mathematics