Skip to main content
Golfed 8 bytes
Source Link
DLosc
  • 39.2k
  • 5
  • 83
  • 141

tinylisp, 125 117117 109 bytes

(d QX(q((N I)(i(l N 1)(e N 0)(QX(s N I)(a I 2
(d S(q((I N)(i(e(a(Q(s NX I) 1)(QX(s IN 1I)1)2)1(i(l N I)0(S(a I 1)N))1
(q((N)(S 0 N

The last line is an anonymous function that takes a number and returns 1 if it is the sum of two squares, 0 otherwise. Try it online!Try it online!

First, we define a helper function QX that determines whethertakes a number N and determines if it is (not) a square. Since we only have builtins for addition and subtraction, we'll use the property of squares that they areA perfect square is the sum of consecutive odd numbers. In other wordsnumbers; therefore, subtracting the sum of the first several odd numbers from N (for an appropriate value of "several") will result in 0 if N is square. Thus, we recurse over increasingly large odd numbers (which we track as I) and subtract each one from N until N equals 0 (in which case N is square) or N is less than 0 (in which case N is not square):

(def not-square?            ; Define not-square?
  (lambda (N I)         ; as a function of two arguments:
    (if (less? N 1)     ;  If N is less than 1,
      (equal? N 0)                ;  return 1N if(0 Nif equalssquare, 0negative andif 0not otherwisesquare)
      (not-square?          ;  Else, recurse with these arguments:
        (sub2 N I)      ;   New N is previous N minus current odd number
        (add2 I 2)))))  ;   New I is the next odd number

Next, we'll define a function S that determines whether a number N is the sum of two squares. Our algorithm here is to recurse over integers I starting at 0: if N minus I is not square, andor IN is also square, thenminus NI is the sum of two squares; otherwisenot square, try the next I until N is less than I, at which point N cannot be the sum of two squares. On the other hand, if I and N minus I are both square, then N is the sum of two squares.

(def sum-squares?                 ; Define sum-squares?
  (lambda (I N)                   ; as a function of two arguments:
    (if                     ;  If
      (add2                 ;  If the sum of
        (equalnot-square? 2       ;    0 if
          (sub2 N I)        ;   2 equalsN minus I
        (add2  1)                ;   ; is square, the< sum0 ofotherwise
          (square? (sub2 N I) 1)  ;   test if N minus I is a square
  ;   and
        (not-square? I 1)))         ;   and test0 if I is a square
  , < 0 otherwise
 1                           ;  thenis returntruthy 1(nonzero), then:
      (if (less? N I)             ;  Else, ifIf N is less than I
        0                         ;   then return 0
        (sum-squares?             ;   Else, recurse with these arguments:
          (add2 I 1)              ;    New I is the next integer
          N)))))               ;    Same N
      1                     ;  Else (I Sameand N minus I are both square), return 1

tinylisp, 125 117 bytes

(d Q(q((N I)(i(l N 1)(e N 0)(Q(s N I)(a I 2
(d S(q((I N)(i(e(a(Q(s N I)1)(Q I 1))2)1(i(l N I)0(S(a I 1)N
(q((N)(S 0 N

The last line is an anonymous function that takes a number and returns 1 if it is the sum of two squares, 0 otherwise. Try it online!

First, we define a helper function Q that determines whether a number N is a square. Since we only have builtins for addition and subtraction, we'll use the property of squares that they are the sum of consecutive odd numbers. In other words, subtracting the sum of the first several odd numbers from N (for an appropriate value of "several") will result in 0 if N is square. Thus, we recurse over increasingly large odd numbers (which we track as I) and subtract each one from N until N equals 0 (in which case N is square) or N is less than 0 (in which case N is not square):

(def square?            ; Define square?
  (lambda (N I)         ; as a function of two arguments:
    (if (less? N 1)     ;  If N is less than 1,
      (equal? N 0)      ;  return 1 if N equals 0 and 0 otherwise
      (square?          ;  Else, recurse with these arguments:
        (sub2 N I)      ;   New N is previous N minus current odd number
        (add2 I 2)))))  ;   New I is the next odd number

Next, we'll define a function S that determines whether a number N is the sum of two squares. Our algorithm here is to recurse over integers I starting at 0: if N minus I is square, and I is also square, then N is the sum of two squares; otherwise, try the next I until N is less than I, at which point N cannot be the sum of two squares.

(def sum-squares?                 ; Define sum-squares?
  (lambda (I N)                   ; as a function of two arguments:
    (if                           ;  If
      (equal? 2                   ;   2 equals
        (add2                     ;   the sum of
          (square? (sub2 N I) 1)  ;   test if N minus I is a square
          (square? I 1)))         ;   and test if I is a square
      1                           ;  then return 1
      (if (less? N I)             ;  Else, if N is less than I
        0                         ;  then return 0
        (sum-squares?             ;  Else, recurse with these arguments:
          (add2 I 1)              ;   New I is the next integer
          N)))))                  ;   Same N

tinylisp, 125 117 109 bytes

(d X(q((N I)(i(l N 1)N(X(s N I)(a I 2
(d S(q((I N)(i(a(X I 1)(X(s N I)1))(i(l N I)0(S(a I 1)N))1
(q((N)(S 0 N

The last line is an anonymous function that takes a number and returns 1 if it is the sum of two squares, 0 otherwise. Try it online!

First, we define a helper function X that takes a number N and determines if it is (not) a square. A perfect square is the sum of consecutive odd numbers; therefore, subtracting the sum of the first several odd numbers from N (for an appropriate value of "several") will result in 0 if N is square. Thus, we recurse over increasingly large odd numbers (which we track as I) and subtract each one from N until N equals 0 (in which case N is square) or N is less than 0 (in which case N is not square):

(def not-square?        ; Define not-square?
  (lambda (N I)         ; as a function of two arguments:
    (if (less? N 1)     ;  If N is less than 1,
      N                 ;  return N (0 if square, negative if not square)
      (not-square?      ;  Else, recurse with these arguments:
        (sub2 N I)      ;   New N is previous N minus current odd number
        (add2 I 2)))))  ;   New I is the next odd number

Next, we'll define a function S that determines whether a number N is the sum of two squares. Our algorithm here is to recurse over integers I starting at 0: if I is not square, or N minus I is not square, try the next I until N is less than I, at which point N cannot be the sum of two squares. On the other hand, if I and N minus I are both square, then N is the sum of two squares.

(def sum-squares?           ; Define sum-squares?
  (lambda (I N)             ; as a function of two arguments:
    (if                     ;  If
      (add2                 ;   the sum of
        (not-square?        ;    0 if
          (sub2 N I)        ;    N minus I
          1)                ;    is square, < 0 otherwise
                            ;   and
        (not-square? I 1))  ;    0 if I is square, < 0 otherwise
                            ;  is truthy (nonzero), then:
      (if (less? N I)       ;   If N is less than I
        0                   ;   then return 0
        (sum-squares?       ;   Else, recurse with these arguments:
          (add2 I 1)        ;    New I is the next integer
          N))               ;    Same N
      1                     ;  Else (I and N minus I are both square), return 1
Golfed 8 bytes
Source Link
DLosc
  • 39.2k
  • 5
  • 83
  • 141

tinylisp, 125125 117 bytes

(d Q(q((I N AI)(i(l A N 1)(Q(ae IN 20)N(aQ(s AN I))(ea AI N2
(d S(q((I N)(i(e(a(Q 1(s N I)01)(Q 1 I 01))2)1(i(l N I)0(S(a I 1)N
(q((N)(S 0 N

The last line is an anonymous function that takes a number and returns 1 if it is the sum of two squares, 0 otherwise. Try it online!Try it online!

First, we define a helper function Q that determines whether a number N is a square. The approachSince we only have builtins for addition and subtraction, we'll use the property of squares that they are the sum of consecutive odd numbers. In other words, subtracting the sum of the first several odd numbers from N (for an appropriate value of "several") will result in 0 if N is tosquare. Thus, we recurse over increasingly large squareodd numbers (which we track as AI until) and subtract each one from N equalsuntil AN equals 0 (in which case N is square) or N is less than A0 (in which case N is not square). Since we only have builtins for addition and subtraction, we'll use the property of squares that they are the sum of consecutive odd numbers (which we track as I):

(def square?            ; Define square?
  (lambda (I N AI)         ; as a function of threetwo arguments:
    (if (less? A N 1)     ;  If AN is less than N1,
      (squareequal?  N 0)      ;  do a recursive call with these arguments:
        (add2 I 2)     ; return 1 Newif IN isequals the0 nextand odd0 numberotherwise
        N  (square?          ;  ;Else, recurse with Samethese Narguments:
        (add2sub2 AN I))      ;   New AN is previous AN plusminus current odd number
        (equal?add2 AI N2)))))  ;  Else, return 1 ifNew AI equalsis Nthe andnext 0odd otherwisenumber
(def sum-squares?                   ; Define sum-squares?
  (lambda (I N)                     ; as a function of two arguments:
    (if                             ;  If
      (equal? 2                     ;   2 equals
        (add2                       ;   the sum of
          (square? 1 (sub2 N I) 01)  ;   test if N minus I is a square
          (square? 1 I 01)))         ;   and test if I is a square
      1                             ;  then return 1
      (if (less? N I)               ;  Else, if N is less than I
        0                           ;  then return 0
        (sum-squares?               ;  Else, recurse with these arguments:
          (add2 I 1)                ;   New I is the next integer
          N)))))                    ;   Same N

tinylisp, 125 bytes

(d Q(q((I N A)(i(l A N)(Q(a I 2)N(a A I))(e A N
(d S(q((I N)(i(e(a(Q 1(s N I)0)(Q 1 I 0))2)1(i(l N I)0(S(a I 1)N
(q((N)(S 0 N

The last line is an anonymous function that takes a number and returns 1 if it is the sum of two squares, 0 otherwise. Try it online!

First, we define a helper function Q that determines whether a number N is a square. The approach is to recurse over increasingly large square numbers A until N equals A (in which case N is square) or N is less than A (in which case N is not square). Since we only have builtins for addition and subtraction, we'll use the property of squares that they are the sum of consecutive odd numbers (which we track as I):

(def square?           ; Define square?
  (lambda (I N A)      ; as a function of three arguments:
    (if (less? A N)    ;  If A is less than N,
      (square?         ;  do a recursive call with these arguments:
        (add2 I 2)     ;   New I is the next odd number
        N              ;   Same N
        (add2 A I))    ;   New A is previous A plus current odd number
      (equal? A N))))  ;  Else, return 1 if A equals N and 0 otherwise
(def sum-squares?                   ; Define sum-squares?
  (lambda (I N)                     ; as a function of two arguments:
    (if                             ;  If
      (equal? 2                     ;   2 equals
        (add2                       ;   the sum of
          (square? 1 (sub2 N I) 0)  ;   test if N minus I is a square
          (square? 1 I 0)))         ;   and test if I is a square
      1                             ;  then return 1
      (if (less? N I)               ;  Else, if N is less than I
        0                           ;  then return 0
        (sum-squares?               ;  Else, recurse with these arguments:
          (add2 I 1)                ;   New I is the next integer
          N)))))                    ;   Same N

tinylisp, 125 117 bytes

(d Q(q((N I)(i(l N 1)(e N 0)(Q(s N I)(a I 2
(d S(q((I N)(i(e(a(Q(s N I)1)(Q I 1))2)1(i(l N I)0(S(a I 1)N
(q((N)(S 0 N

The last line is an anonymous function that takes a number and returns 1 if it is the sum of two squares, 0 otherwise. Try it online!

First, we define a helper function Q that determines whether a number N is a square. Since we only have builtins for addition and subtraction, we'll use the property of squares that they are the sum of consecutive odd numbers. In other words, subtracting the sum of the first several odd numbers from N (for an appropriate value of "several") will result in 0 if N is square. Thus, we recurse over increasingly large odd numbers (which we track as I) and subtract each one from N until N equals 0 (in which case N is square) or N is less than 0 (in which case N is not square):

(def square?            ; Define square?
  (lambda (N I)         ; as a function of two arguments:
    (if (less? N 1)     ;  If N is less than 1,
      (equal? N 0)      ;  return 1 if N equals 0 and 0 otherwise
      (square?          ;  Else, recurse with these arguments:
        (sub2 N I)      ;   New N is previous N minus current odd number
        (add2 I 2)))))  ;   New I is the next odd number
(def sum-squares?                 ; Define sum-squares?
  (lambda (I N)                   ; as a function of two arguments:
    (if                           ;  If
      (equal? 2                   ;   2 equals
        (add2                     ;   the sum of
          (square? (sub2 N I) 1)  ;   test if N minus I is a square
          (square? I 1)))         ;   and test if I is a square
      1                           ;  then return 1
      (if (less? N I)             ;  Else, if N is less than I
        0                         ;  then return 0
        (sum-squares?             ;  Else, recurse with these arguments:
          (add2 I 1)              ;   New I is the next integer
          N)))))                  ;   Same N
Source Link
DLosc
  • 39.2k
  • 5
  • 83
  • 141

tinylisp, 125 bytes

(d Q(q((I N A)(i(l A N)(Q(a I 2)N(a A I))(e A N
(d S(q((I N)(i(e(a(Q 1(s N I)0)(Q 1 I 0))2)1(i(l N I)0(S(a I 1)N
(q((N)(S 0 N

The last line is an anonymous function that takes a number and returns 1 if it is the sum of two squares, 0 otherwise. Try it online!

Ungolfed/explanation

Look, ma, no library!

First, we define a helper function Q that determines whether a number N is a square. The approach is to recurse over increasingly large square numbers A until N equals A (in which case N is square) or N is less than A (in which case N is not square). Since we only have builtins for addition and subtraction, we'll use the property of squares that they are the sum of consecutive odd numbers (which we track as I):

(def square?           ; Define square?
  (lambda (I N A)      ; as a function of three arguments:
    (if (less? A N)    ;  If A is less than N,
      (square?         ;  do a recursive call with these arguments:
        (add2 I 2)     ;   New I is the next odd number
        N              ;   Same N
        (add2 A I))    ;   New A is previous A plus current odd number
      (equal? A N))))  ;  Else, return 1 if A equals N and 0 otherwise

Next, we'll define a function S that determines whether a number N is the sum of two squares. Our algorithm here is to recurse over integers I starting at 0: if N minus I is square, and I is also square, then N is the sum of two squares; otherwise, try the next I until N is less than I, at which point N cannot be the sum of two squares.

(def sum-squares?                   ; Define sum-squares?
  (lambda (I N)                     ; as a function of two arguments:
    (if                             ;  If
      (equal? 2                     ;   2 equals
        (add2                       ;   the sum of
          (square? 1 (sub2 N I) 0)  ;   test if N minus I is a square
          (square? 1 I 0)))         ;   and test if I is a square
      1                             ;  then return 1
      (if (less? N I)               ;  Else, if N is less than I
        0                           ;  then return 0
        (sum-squares?               ;  Else, recurse with these arguments:
          (add2 I 1)                ;   New I is the next integer
          N)))))                    ;   Same N

Finally, our submission is an anonymous function that takes N only and passes it to sum-squares? with a starting I of 0:

(lambda (N)
  (sum-squares? 0 N))