OFFSET
1,1
LINKS
Bi Cheng Wu, Table of n, a(n) for n = 1..4958 (a(n) < 1000000)
EXAMPLE
112 -> 1+1=2;
1235 -> 1+2=3 and 2+3=5;
224610 -> 2+2=4 and 2+4=6 and 4+6=10.
MATHEMATICA
part[n_] := part[n] = Select[Flatten[ Permutations /@ Reverse /@ IntegerPartitions[n, {3, n}], 1], 0 <= #[[3]] - Max[#[[1]], #[[2]]] <= 1 && AllTrue[Rest@ Rest@ Differences@ #, 0 <= # <= 1 &] &]; spl[x_, L_] := Map[ FromDigits@ Take[x, #] &, Transpose[{Most@ #, Rest[#]-1}& [ FoldList[ Plus, 1, L]]]]; sumQ[w_] := AllTrue[Range[3, Length@w], w[[#]] == w[[#-1]] + w[[#-2]] &]; ok[n_] := Block[{m = IntegerLength@ n}, AnyTrue[ spl[ IntegerDigits[n], #] & /@ part[m], sumQ[#] && Total[ IntegerLength /@ #] == m &]]; Select[ Range[100, 6000], ok] (* Giovanni Resta, Dec 03 2019 *)
PROG
(Python)
def isSegmentSum(digits, segment1=None, segment2=None):
digits = str(digits)
N = len(digits)
if N == 0:
return True
else:
if (segment1 is None) and (segment2 is None):
for i in range(N):
try:
slice1 = digits[:i+1]
for j in range(N-(i+1)):
slice2 = digits[i+1:i+1+j+1]
slice3 = digits[i+1+j+1:]
if (isSegmentSum(slice3, slice1, slice2) and
len(slice3)>0 and not (slice1.startswith("0") or
slice2.startswith("0") or
(slice3.startswith("0")))):
return True
except:
return False
else:
sumOfDigits = str(int(segment1)+int(segment2))
nS = len(sumOfDigits)
try:
if digits[:nS] == sumOfDigits:
return isSegmentSum(digits[nS:], segment2, digits[:nS])
else:
return False
except:
return False
return False
def findSegmentSum(lower, upper):
for i in range(lower, upper+1):
if isSegmentSum(i):
print(str(i))
findSegmentSum(1, 5200)
CROSSREFS
KEYWORD
nonn,base
AUTHOR
Bi Cheng Wu, Nov 19 2019
STATUS
approved