cp's OEIS Frontend

This is a front-end for the Online Encyclopedia of Integer Sequences, made by Christian Perfect. The idea is to provide OEIS entries in non-ancient HTML, and then to think about how they're presented visually. The source code is on GitHub.

Showing 1-2 of 2 results.

A002828 Least number of squares that add up to n.

Original entry on oeis.org

0, 1, 2, 3, 1, 2, 3, 4, 2, 1, 2, 3, 3, 2, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 3, 1, 2, 3, 4, 2, 3, 4, 2, 3, 2, 3, 1, 2, 3, 4, 2, 2, 3, 3, 3, 2, 3, 4, 3, 1, 2, 3, 2, 2, 3, 4, 3, 3, 2, 3, 4, 2, 3, 4, 1, 2, 3, 3, 2, 3, 3, 4, 2, 2, 2, 3, 3, 3, 3, 4, 2, 1, 2, 3, 3, 2, 3, 4, 3, 2, 2, 3, 4, 3, 3, 4, 3, 2, 2, 3, 1, 2, 3, 4, 2, 3
Offset: 0

Views

Author

Keywords

Comments

Lagrange's "Four Squares theorem" states that a(n) <= 4.
It is easy to show that this is also the least number of squares that add up to n^3.
a(n) is the number of iterations in f(...f(f(n))...) to reach 0, where f(n) = A262678(n) = n - A262689(n)^2. Allows computation of this sequence without Lagrange's theorem. - Antti Karttunen, Sep 09 2016
It is also easy to show that a(k^2*n) = a(n) for k > 0: Clearly a(k^2*n) <= a(n) but for all 4 cases of a(n) there is no k which would result in a(k^2*n) < a(n). - Peter Schorn, Sep 06 2021

References

  • N. J. A. Sloane, A Handbook of Integer Sequences, Academic Press, 1973 (includes this sequence).
  • N. J. A. Sloane and Simon Plouffe, The Encyclopedia of Integer Sequences, Academic Press, 1995 (includes this sequence).

Crossrefs

Programs

  • Haskell
    a002828 0 = 0  -- confessedly  /= 1, as sum [] == 0
    a002828 n | a010052 n == 1 = 1
              | a025426 n > 0 = 2 | a025427 n > 0 = 3 | otherwise = 4
    -- Reinhard Zumkeller, Feb 26 2015
    
  • Maple
    with(transforms);
    sq:=[seq(n^2, n=1..20)];
    LAGRANGE(sq,4,120);
    # alternative:
    f:= proc(n) local F,x;
       if issqr(n) then return 1 fi;
       if nops(select(t -> t[1] mod 4 = 3 and t[2]::odd, ifactors(n)[2])) = 0 then return 2 fi;
       x:= n/4^floor(padic:-ordp(n,2)/2);
       if x mod 8 = 7 then 4 else 3 fi
    end proc:
    0, seq(f(n),n=1..200); # Robert Israel, Jun 14 2016
    # next Maple program:
    b:= proc(n, i) option remember; convert(series(`if`(n=0, 1, `if`(i<1, 0,
          b(n, i-1)+(s-> `if`(s>n, 0, x*b(n-s, i)))(i^2))), x, 5), polynom)
        end:
    a:= n-> ldegree(b(n, isqrt(n))):
    seq(a(n), n=0..105);  # Alois P. Heinz, Oct 30 2021
  • Mathematica
    SquareCnt[n_] := If[SquaresR[1, n] > 0, 1, If[SquaresR[2, n] > 0, 2, If[SquaresR[3, n] > 0, 3, 4]]]; Table[SquareCnt[n], {n, 150}] (* T. D. Noe, Apr 01 2011 *)
    sc[n_]:=Module[{s=SquaresR[Range[4],n]},If[First[s]>0,1,Length[ First[ Split[ s]]]+1]]; Join[{0},Array[sc,110]] (* Harvey P. Dale, May 21 2014 *)
  • PARI
    istwo(n:int)=my(f);if(n<3,return(n>=0););f=factor(n>>valuation(n, 2)); for(i=1,#f[,1],if(bitand(f[i,2],1)==1&&bitand(f[i,1],3)==3, return(0)));1
    isthree(n:int)=my(tmp=valuation(n,2));bitand(tmp,1)||bitand(n>>tmp,7)!=7
    a(n)=if(isthree(n), if(issquare(n), !!n, 3-istwo(n)), 4) \\ Charles R Greathouse IV, Jul 19 2011, revised Mar 17 2022
    
  • Python
    from sympy import factorint
    def A002828(n):
        if n == 0: return 0
        f = factorint(n).items()
        if not any(e&1 for p,e in f): return 1
        if all(p&3<3 or e&1^1 for p,e in f): return 2
        return 3+(((m:=(~n&n-1).bit_length())&1^1)&int((n>>m)&7==7)) # Chai Wah Wu, Aug 01 2023
    
  • Python
    from sympy.core.power import isqrt
    def A002828(n):
        dp = [-1] * (n + 1)
        dp[0] = 0
        for i in range(1, n + 1):
            S = []
            r = isqrt(i)
            for j in range(1, r + 1):
                S.append(1 + dp[i - (j**2)])
            dp[i] = min(S)
        return dp[-1] # DarĂ­o Clavijo, Apr 21 2025
  • Scheme
    ;; The first one follows Charles R Greathouse IV's PARI-code above:
    (define (A002828 n) (cond ((zero? n) n) ((= 1 (A010052 n)) 1) ((= 1 (A229062 n)) 2) (else (+ 3 (A072401 n)))))
    (define (A229062 n) (- 1 (A000035 (A260728 n))))
    ;; We can also compute this without relying on Lagrange's theorem. The following recursion-formula should be used together with the second Scheme-implementation of A262689 given in the Program section that entry:
    (definec (A002828 n) (if (zero? n) n (+ 1 (A002828 (- n (A000290 (A262689 n)))))))
    ;; Antti Karttunen, Sep 09 2016
    

Formula

From Antti Karttunen, Sep 09 2016: (Start)
a(0) = 0; and for n >= 1, if A010052(n) = 1 [when n is a square], a(n) = 1, otherwise, if A229062(n)=1, then a(n) = 2, otherwise a(n) = 3 + A072401(n). [After Charles R Greathouse IV's PARI program.]
a(0) = 0; for n >= 1, a(n) = 1 + a(n - A262689(n)^2), (see comments).
a(n) = A053610(n) - A062535(n).
(End)

Extensions

More terms from Arlin Anderson (starship1(AT)gmail.com)

A262690 a(n) = largest square k <= n such that A002828(n-k) = A002828(n)-1.

Original entry on oeis.org

0, 1, 1, 1, 4, 4, 4, 4, 4, 9, 9, 9, 4, 9, 9, 9, 16, 16, 9, 9, 16, 16, 9, 9, 16, 25, 25, 25, 25, 25, 25, 25, 16, 25, 25, 25, 36, 36, 36, 36, 36, 25, 25, 25, 36, 36, 36, 36, 16, 49, 49, 49, 36, 49, 49, 49, 36, 49, 49, 49, 49, 36, 49, 49, 64, 64, 64, 49, 64, 64, 36, 49, 36, 64, 49, 49, 36, 64, 49, 49, 64, 81, 81, 81, 64, 81, 81, 81, 36, 64, 81, 81, 81, 64, 81, 81, 64, 81, 49, 81, 100
Offset: 0

Views

Author

Antti Karttunen, Oct 03 2015

Keywords

Crossrefs

Programs

Formula

a(n) = A000290(A262689(n)).
Other identities. For all n >= 0:
A262678(n) = n - a(n).
Showing 1-2 of 2 results.