Write a program/function that when given a positive integer \$n\$ splits the numbers from \$1\$ to \$n\$ into two sets, so that no integers \$a, b, c\$, satisfying \$a^2 + b^2 = c^2\$ are all in the same set. For example, if \$3\$ and \$4\$ are in the first set, then \$5\$ must be in the second set since \$3^2+4^2=5^2\$.

Acceptable Output Formats:

  • One of the sets
  • Both the sets
  • An array of length \$n\$ where the \$i\$-th element (counting from 1) is one of two different symbols (e.g. 0 and 1, a and b, etc.) which represent which set \$i\$ belongs to.The reverse of this is also fine


You can expect \$n\$ to be less than \$7825\$. This is because \$7824\$ is proven to be the largest number to have solution (which also implies that all numbers less than 7825 have a solution).


This is so shortest bytes wins.

Sample Testcases

3 -> {1}
3 -> {}
5 -> {1, 2, 3}
5 -> {1, 2, 3}, {4, 5}
5 -> [0, 0, 0, 1, 1]
5 -> [1, 1, 0, 0, 1]
10 -> {1, 3, 6}
10 -> {1, 2, 3, 4, 6, 9}
41 -> {5, 6, 9, 15, 16, 20, 24, 35}

A checker to verify your output can be found here

Inspired by The Problem with 7825 - Numberphile

  • 1
    \$\begingroup\$ Is it guaranteed that a solution exists for all n below 7825? It may be worth clarifying in the challenge \$\endgroup\$ – Luis Mendo Jun 20 '20 at 16:04
  • \$\begingroup\$ Suggested test case: 41. This is the first value for which this simple but invalid algorithm doesn't work anymore: start with an empty list A; for x = 1 to n: if there's some x in A such that sqrt(x²+n²) also exists in A: leave A unchanged else append x to A; return A. \$\endgroup\$ – Arnauld Jun 20 '20 at 16:08
  • 1
    \$\begingroup\$ @LuisMendo Theorem 1. The set {1, . . . , 7824} can be partitioned into two parts, such that no part contains a Pythagorean triple, while this is impossible for {1, . . . , 7825}. arxiv.org/pdf/1605.00723.pdf \$\endgroup\$ – ZaMoC Jun 20 '20 at 16:33
  • \$\begingroup\$ @Arnauld added 41 as a testcase \$\endgroup\$ – Mukundan314 Jun 20 '20 at 16:52
  • \$\begingroup\$ May we output an empty set when \$n<5\$? (I would assume so.) \$\endgroup\$ – Jonathan Allan Jun 20 '20 at 19:25

J, 37 bytes

Brute forces through the possible sets, outputs the bit mask.


Try it online! (Also outputs list as numbers for easier comparison.)

How it works

                                  #&1 convert to list of N 1's
(                            )^:_     do until list does not change
                        *:@#\         right: convert to 1,4,9…,N^2
             [                        left: the bit mask
                      /.              partition left based on right, for each set:
                  +/~                 make M*M addition table
               e.~                    any element of that in the same set?
       +./@,                          OR all answers: 1 on conflict, 0 if finished
  -&.#.                               list: from base 2, subtract that^, to base 2
  • \$\begingroup\$ I guess 1,2,4…,N^2 is a typo of 1,4,9…,N^2? \$\endgroup\$ – Bubbler Jun 21 '20 at 23:27
  • \$\begingroup\$ @Bubbler Indeed! \$\endgroup\$ – xash Jun 21 '20 at 23:39

Jelly, 18 bytes


Try it online! (too inefficient for \$n>25\$ on TIO).


Strategy: Find all Pythagorean triples using \$[1,n]\$ then find a way to pick 1 element from each of them such that the resulting set contains no Pythagorean triples. That way we have a set which both contains no Pythagorean triple and blocks the other set from having any.

œc3²SHeƊ$Ƈ - Link 1, find all Pythagorean triples: list of integers OR number
œc3        - all combination of length 3 (given n uses [1..n])
         Ƈ - keep those for which:
        $  -   last two links as a monad:
   ²       -     square each of them
       Ɗ   -     last three links as a monad:
    S      -       sum (of the three squares)
     H     -       halved
      e    -       exists in (the squares)?

ÇŒpÇÞḢQ - Main Link: n
Ç       - call Link 1 as a monad -> all Pythagorean triples using [1,n]
 Œp     - Cartesian product -> all ways to pick one from each
    Þ   - sort those by:
   Ç    -   call Link 1 as a monad (empty lists are less than non-empty ones)
     Ḣ  - head
      Q - deduplicate (if n < 7825 this is a valid answer)
  • 1
    \$\begingroup\$ Nice! Certainly runs faster than mine, besides being much shorter \$\endgroup\$ – fireflame241 Jun 20 '20 at 19:30
  • \$\begingroup\$ What is the purpose of ÇÞ? Isn't the Cartesian product automatically in that order? \$\endgroup\$ – fireflame241 Jun 20 '20 at 19:40
  • 2
    \$\begingroup\$ No, once \$n=26\$ the result's first item will contain \$6\$, \$8\$, and \$10\$. (So taking the smallest of each triple is not a valid strategy.) \$\endgroup\$ – Jonathan Allan Jun 20 '20 at 19:45
  • \$\begingroup\$ Oh, I misunderstood how Þ works, and I was about to bring up the counterexample for n=86 ((5,12,13),(12,35,37),(13,84,85)) \$\endgroup\$ – fireflame241 Jun 20 '20 at 19:47
  • 1
    \$\begingroup\$ In that case, I retract my objection - assuming that, as you pick more than one due to repeats, you check that you don't end up picking all of a triple eventually. If you have a valid partition into two sets X and Y, you can just, for each triple, pick an element of that triple that's in X. The set Z of all elements picked is a subset of X (so it doesn't contain all of any triple) but contains one element of each triple by construction. \$\endgroup\$ – Misha Lavrov Jun 21 '20 at 20:13

Wolfram Language (Mathematica), 132 116 bytes


Try it online!

This uses Mathematica's SAT solver to label the integers 1 through the input as True and False.

  • This is composed with Range, so what feeds into the main function is a list of the integers from 1 to the input.
  • Select[#~Tuples~3,{1,1,-1}.#^2==0&] generates all the Pythagorean triples (multiple times actually, but that's okay).
  • And[Or@@#,Nand@@#]& is true if at least one, but not all, of the elements of its input is true.
  • {1}.SatisfiabilityInstances[...,x/@#] uses the SAT solver. Since SatisfiabilityInstances returns a list containing one solution, we use {1}. to get its first element.
  • \$\begingroup\$ Very nice +1! you can use infix notation in ~Subsets~ to save a byte. Also your program must be a function and that costs 3 bytes... Try it online! \$\endgroup\$ – ZaMoC Jun 20 '20 at 21:35
  • \$\begingroup\$ @J42161217 My program is a function, I just haven't finished it with &: it's the composition of two functions, {1}.Satisf....,x/@#]& and Range. Your version composes with Range@#& instead which is equivalent to Range, but longer. \$\endgroup\$ – Misha Lavrov Jun 20 '20 at 21:40
  • \$\begingroup\$ My bad, here is a better presentation 131 bytes! \$\endgroup\$ – ZaMoC Jun 20 '20 at 21:57
  • \$\begingroup\$ I ended up going with Tuples instead, because #~Tuples~3 is shorter than #~Subsets~{3}. It also generates way more triples, and even after the Select we end up getting {3,4,5} and {4,3,5} separately, but that's fine. \$\endgroup\$ – Misha Lavrov Jun 20 '20 at 22:07

JavaScript (ES6),  118  117 bytes

Much slower for -1 byte.


Try it online!

JavaScript (ES6),  122 119  118 bytes

Returns one of the sets as an array.


Try it online!

Solution found locally for \$n=41\$:

[ 5, 6, 9, 15, 16, 20, 24, 35 ]

05AB1E, 14 bytes

Port of the 17 byte Jelly answer. (Læ3ùʒDnO;tå}€н is the same length)


Try it online!


L              Length range
 æ             Powerset
  3ù           Pick truples (length-3 tuples)
    ʒ          Filter:
     n             Square all items
      R            Reverse the list
       ć           Head-extract (head on top)
        s          Swap
         O         Sum the remaining list
          Q}       Equal?
            €н Take head of each
  • \$\begingroup\$ Not sure how to fix it, but it's currently incorrect for certain inputs \$\geq26\$, just like Jonathan's initial answer. PS: using 3.Æ is faster than æ3ù, although it doesn't matter for the byte-count. :) \$\endgroup\$ – Kevin Cruijssen Jun 22 '20 at 7:21

Jelly, 30 26 bytes


Try it online!


This does a more brute-force approach, filtering subsets of [1..n] based on whether they contain any Pythagorean triples. Then, it finds two triple-less subsets that have all n elements between them

œ|/L=³         # Test if a pair of sets unions to [1..n]
œ|/              # Set intersection  
   L             # Is the length
    =³           # equal to n?       

Œc§œ&          # Does a pair exist that sums to another?
Œc               # Compute all pairs of squares
  §              # Sum each
   œ&            # Set intersection with the set of squares (nonempty & truthy if a pair of squares sum to another square)

ŒP²ÇẸƊÐḟŒcÑƇḢ  # Main link
ŒP               # All subsets of 1..n
     ƊÐḟ         # Remove those where:
  ²                # of the squares,
   ÇẸ              # a pair of the squares exists that sum to another square
        Œc       # All pairs of these triple-less subsets
          ÑƇ     # Filter the pairs by whether they union to [1..n]
            Ḣ    # Head; get the first one

Wolfram Language (Mathematica), 1664 bytes

works for all n (1 to 7824) instantly


Try it online!


R, 99 95 bytes


Try it online!

Outputs a vector of TRUE and FALSE representing in reverse order which set each integer belongs to. (The footer of the TIO transforms this into a list of integers in the first set.)

Works by random sampling: repeatedly draw a random subset of 1:n until neither the subset nor its complement contain any Pythagorean triples (checked by the function f).

It will finish in finite time for any input <7825, but will in expectation take a very long time for largeish n. TIO starts timing out around n=90.


Charcoal, 74 bytes


Try it online! Well, for n<50, otherwise it gets too slow. Link is to verbose version of code. Based on @JonathanAllen's answer. Explanation:


Input n.


Loop through all potential Pythagorean triples.


If this is indeed a triple,


then push it to the empty list.


Start iterating through the ways of picking one element of each triple.


Repeat until output has been generated.


Pick one element from each triple.


Increment the loop counter.


Square the elements.


Check for Pythagorean triples.


If none, then output one of the sets.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.