The sum of N consecutive integers, where the smallest one is "a" is given by:
(a - 1/2)N + N^2/2 = target = 3^11 in our problem.
I made a series of small functions in Python solving for target, a, and N in terms of the other two.
Although a is generally an integer, the function that calculates 'a' doesn't assume that. It calculates an exact value for a(N,target), which for most values of N and target will not be an integer.
The program then starts at the largest possible value for N, then decrements by 1. It then checks to see if the calculated value for a is an integer, and if so, prints it out.
The maximum value for N is then 486, with the first term in the series, a, equal to 122.
Note that if the word "positive" were eliminated from the stipulations:
N = 354,294 with a starting integer of (1 - 3^11) = -177,146
---- Python code follows ----
target = 3**11
def ceiling(x):
return int((-x) // 1 * -1)
def sumConsec(a,N):
""" sum integers a + (a+1) + ... + (a+N-1) """
return ((a-.5)*N + N*N/2)
def upper_bound_of_N(a, target=3**11):
return ceiling( .5-a + ( a*a - a +.25 + 2*(target) )**.5 )
def calc_real_a(N, target=3**11):
return .5 + (target)/N - N/2
print ( ' N ', ' a ')
for i in range(upper_bound_of_N(1),0,-1):
if calc_real_a(i) == round(calc_real_a(i)):
print(i, int(calc_real_a(i)) )
output:
N a
486 122
243 608
162 1013
81 2147
54 3254
27 6548
18 9833
9 19679
6 29522
3 59048
2 88573
1 177147
|
Posted by Larry
on 2021-03-13 14:15:36 |