10
\$\begingroup\$

A sign sequence is an infinite sequence consisting entirely of \$1\$ and \$-1\$. These can be constructed a number of ways, for example:

  • Alternating signs: \$1, -1, 1, -1, ...\$
  • \$-1\$ for primes, \$1\$ for non-primes: \$1, -1, -1, 1, -1, 1, -1, ...\$
  • All \$1\$s: \$1, 1, 1, ...\$

Your task is to write a piece of code that outputs a deterministic sign sequence that no other answer already outputs. You must include a proof that your sequence is unique from all sequences posted before yours and that your sequence only contains \$1\$ and \$-1\$. You do not have to worry about keeping up to date for newer answers, as they must ensure their sequences are unique, not you.

You may output in any reasonable manner, including (but not limited to):

  • Outputting an infinite list/generator/tuple of values
  • Outputting the next value in the sequence each time your code is run
  • Outputting the sequence infinitely

You may not take any input (unless necessary), so outputing the first \$n\$ terms or the \$n\$th term is not allowed.

I've included my implementation of a sequence as the first answer, to ensure that all answers have to provide a proof of uniqueness.


This is a , so the answer with the most votes wins. You should aim to do the following things in your answer:

  • Be creative. Avoid simply outputting constant runs of \$1\$s or \$-1\$s or outputting one value when a number is insert common numeric property here and the other when not (e.g. primes or Fibonacci numbers).
  • Avoid copying others. While all sequences must be unique, aim to be innovative, rather than simply slightly modify another user's sequence (for example, swapping the placements of \$1\$ and \$-1\$)
  • Make it clear what your program is doing. Not everyone can read a Jelly, R or Java answer, but they can read an explanation of the answer, as well as an explanation of how/why you chose this specific sequence and the proof of uniqueness included in your answer

Voters should consider the following when casting their votes:

  • How creative is the sequence? Has it been done to death before, or is it something you've never seen before?

    • Is the sequence using some properties of \$1\$ and \$-1\$ to be generated, or is it just applying the \$\text{sgn}\$ function to other sequences?
  • Is it unique, or is it simply a slight modification on a sequence that many other users have done? If it is a modification, is it uncreative, or has the author seen a property that others haven't?

  • How clever is the implementation of the sequence, and how well explained is it? For this, consider both the actual code of the answer and the algorithm it implements. If the code uses a language specific trick you find particularly impressive, it may be worth an upvote. If the implementation of the algorithm is so general than any language could be used, yet is still creative and unique, it's probably worth an upvote. However, if the code is overly convoluted when a simpler method would work, or if the algorithm is incredibly inefficient when a better version exists, consider casting a downvote.

    Furthermore, while you may not be able to understand the 10 bytes of 05AB1E posted, if explained well, you should be able to get a solid understanding of how those 10 bytes implement the chosen sequence, and how clever it is. And while you may be able to fluently read Python, if poorly coded with no explanation, you may not be able to fully understand how that program works. Consider this factor when voting.

Voters should not vote for an answer for any of the following reasons:

  • The program is written in your favourite/least favourite language
    • Voting for the use of tricks within a language are fine. Voting for an answer because of the language, is not an acceptable reason
  • The program is short/long/written with ASCII characters/written without ASCII characters
  • You recognize the user who wrote the answer and you love/hate them
  • Any other reason not specified above (e.g. "This answer uses the e character, I love it!")
\$\endgroup\$

19 Answers 19

16
\$\begingroup\$

05AB1E, 14 bytes

∞v®yÒgm=Ox<.±,

Try it online!

My first non-trivial 05AB1E answer! Happy for suggestions to improve it.

The code prints two interleaved sign sequences, both related to the Pólya conjecture. In 1919, George Pólya conjectured that the majority (no less than half) of positive integers up to any finite limit \$\ge2\$ have an odd number of prime factors, counted with multiplicity. Mathematically, the summatory Liouville function \$L(n)\$ (A002819) was posited to satisfy the inequality $$ L(n)=\sum_{k=1}^n\lambda(k)\le0\ $$ for all \$n\ge2\$, wherein $$ \lambda(k)=(-1)^{\Omega(k)}=\begin{cases}-1,\ \text{$k$ has an odd number of prime factors}\\\phantom{-}1,\ \text{$k$ has an even number of prime factors}\end{cases} $$ is the Liouville function (A008836), related to the parity of the number of prime factors of \$k\$, and \$\Omega(k)\$ is the prime-factor-counting omega function that respects multiplicity.

The Pólya conjecture was shown to be false in 1958 when Brian Haselgrove proved the existence of an enormous counterexample near \$n=\exp(831.847)\approx1.845\times10^{361}\$. Subsequently, it has been shown that the smallest counterexample is \$n=906\,150\,257\$, and that the conjecture fails for most values of \$n\$ in the range \$906\,150\,257\le n \le 906\,488\,079\$. It is also now known that \$L(n)\$ changes sign infinitely often.

The code prints two values for each integer \$n\ge1\$: $$ \DeclareMathOperator{\sgn}{sgn} \begin{gather} \lambda(n), \tag{1} \\ \sgn\bigl[2L(n)-1\bigr]. \tag{2} \end{gather} $$ The first value is the \$n\$th term of A008836. The second value, chosen to avoid zeros in \$L(n)\$, is (arguably) more interesting. Given the results quoted above, we see that sequence \$(2)\$ alternates between \$-1\$ and \$1\$ infinitely often. However, of its first \$906\,150\,257\$ values, only two (the first and the last) are \$1\$; the intervening \$906\,150\,255\$ values are all \$-1\$.

Commented code

∞v             # iterate over all positive integers y
  ®            # push -1
   yÒ          # push list of prime factors of y (with duplicates)
     g         # length of this list; yields Ω(y)
      m        # exponentiate top two stack items; yields -1**Ω(y) = λ(y)
       =       # print λ(y), keeping it on the stack
        O      # sum the stack; yields L(y)
         x<    # push 2*L(y)-1, keeping L(y) on the stack
           .±, # print the sign
\$\endgroup\$
7
\$\begingroup\$

Wolfram Language (Mathematica)

primesUpTo[n_] := Select[Range[n], PrimeQ];
sumsOfTwoPrimes[n_] := Union @@ Outer[Plus, primesUpTo[n], primesUpTo[n]];
GoldbachConjectureHoldsFor[n_] := MemberQ[sumsOfTwoPrimes[n], n];
BooleanToSign[TrueOrFalse_] := 2 Boole[TrueOrFalse] - 1;

Do[Print[ BooleanToSign@GoldbachConjectureHoldsFor[2n] ], {n, ∞}]

Try it online!

  • primesUpTo generates a list of all primes up to n, and then sumsOfTwoPrimes generates all sums of two such primes.
  • This allows GoldbachConjectureHoldsFor to check whether n is the sum of two primes. (The function works for all n, though the Goldbach conjecture itself is concerned only with even n.)
  • GoldbachConjectureHoldsFor returns True or False, which BooleanToSign converts to 1 or -1 respectively.
  • Therefore BooleanToSign@GoldbachConjectureHoldsFor[2n] returns 1 if the Goldbach conjecture holds for 2n and -1 otherwise.
  • The Do[Print[ ... ], {n, ∞}] prints (in principle) the infinite list of results.

For those looking not to repeat earlier sequences, this sequence starts with a -1 and then contains only 1s forever thereafter ... or does it?!

\$\endgroup\$
7
\$\begingroup\$

Python 3, 91 bytes

s=b's=%r;q=s%%s\nwhile[print(c%%2*2-1)for c in s%%s]:0'
while[print(c%2*2-1)for c in s%s]:0

Try it online!

Quine, which outputs 1 or -1 according to the last bit of each byte of the quine, repeating infinitely. Starts 1 1 -1 1 1 1 1 -1 1 1

Previously 107 bytes. I know it's not , but I couldn't resist golfing it a bit. This does change the sequence slightly but no other sequences are the same so it doesn't affect the competition

\$\endgroup\$
6
\$\begingroup\$

C (gcc), 38 bytes

int f() {
  return (rand() & 2) - 1;
}

Try it online!

This could be golfed into 21 bytes (f(n){n=(rand()&2)-1;}) but this question is not tagged as .

In C, random without seeds behave deterministically which actually fit the requirement of this question. I don't know how the sequence is generated. But it does generate a list which only contains 1's and -1's, some how.

First 100 generated values are:

 1  1 -1  1 -1  1  1 -1 -1 -1 
 1  1  1  1  1  1 -1  1 -1 -1 
 1 -1  1 -1  1  1  1  1  1  1 
-1  1  1  1 -1  1 -1 -1  1  1 
-1 -1 -1  1 -1 -1  1 -1  1  1 
-1  1  1 -1 -1 -1  1  1 -1 -1 
-1 -1 -1  1 -1 -1  1 -1 -1 -1 
-1  1  1 -1  1  1  1 -1  1  1 
 1 -1 -1  1 -1 -1  1  1  1  1 
-1 -1 -1 -1  1 -1  1  1  1  1 
\$\endgroup\$
3
\$\begingroup\$

Jelly, 8 bytes

‘ÆTṠṄaƲß

Try it online!

This outputs the series generated by \$f(n) = \text{sgn}(\tan(n+1))\$ as \$n = 0, 1, 2, 3, ...\$, separated by newlines. This only yields \$0\$ iff \$\tan(n+1) = 0\$, which only happens when \$n = 2k\pi-1\$ for some integer \$k\$. As this is only an integer when \$k = 0\$ and \$n = -1\$, this never happens.

How it works

‘ÆTṠṄaƲß - Main link. Takes an integer n (initially 0) on the left
‘        - Yield n+1
      Ʋ  - To n+1:
 ÆT      -   tan(n+1)
   Ṡ     -   sgn(tan(n+1))
    Ṅ    -   Print and return sgn(tan(n+1))
     a   -   Replace sgn(tan(n+1)) with n+1
       ß - Recursively call 
\$\endgroup\$
3
\$\begingroup\$

Python 3

def gray(n):
  return n ^ n>>1

previous = n = 0
while True:
    n += 1
    bitcount = bin(gray(n)).count('1')
    print(bitcount - previous)
    previous = bitcount

Try it online!

Sequence starts with 1 1 -1 1 1 -1 -1 1 1 1 -1.

These are the differences in the number of set bits between adjacent gray codes. This happens to be the same sequence as:

  1. Start with a single 1
  2. Reverse the current sequence of 1 and -1 and negate every value. Join the original and modified sequence with a 1. Repeat step 2.

1 -> 1 1 -1 -> 1 1 -1 1 1 -1 -1 -> 1 1 -1 1 1 -1 -1 1 1 1 -1 -1 1 -1 -1 -> ...

Bonus implementation in Coconut using this construction:

sequence = [1]
print(1)
while True:
    new = sequence |> reversed |> map$(-) |> list |> x->[1]+x
    new |*> print$(sep='\n')
    sequence.extend(new)

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Haskell, 55 bytes

a=1: -1:(zipWith(*)a$tail b)
b=1:1:(zipWith(*)b$tail a)

Try it online!

a is the infinite sequence here. Neither is very interesting - a is [1,-1,1,1,1,-1] repeated forever and b is [1,1,-1,1,-1,1] repeated forever. However, I think this shows how cool Haskell's laziness is: you have two infinite lists dependent on each other. a starts with 1 and -1, and the rest of it is found by multiplying a by the tail of b. Similarly, b starts with 1 and 1, and the rest of it is found by multiplying b by the tail of a.

So the third element of a would be 1 * 1 = 1 and for b it would be 1 * -1 = -1. The fourth elements would be -1 * -1 = 1 for a and 1 * 1 = 1 for b, and so on.

\$\endgroup\$
  • \$\begingroup\$ Would you mind making a definitive choice as to which of a and b is your sequence, so that it's easier for others to follow the "sequences must be unique" rule? \$\endgroup\$ – caird coinheringaahing Jan 2 at 20:36
  • \$\begingroup\$ Oh yeah, of course \$\endgroup\$ – user100177 Jan 2 at 20:36
2
\$\begingroup\$

Pyth, 10 bytes

#V=hZ_1VZ1

Try it online!


Sequence is -1,1,-1,1,1,-1,1,1,-1,1,1,1,-1,1,1,1,-1,1,1,1..., or (-1,(1)*n)*n for n in [1, ∞).

\$\endgroup\$
2
\$\begingroup\$

Bash, 5 bytes

This outputs all items in the sequence \$1, 1, 1, 1, ...\$ ... the rules do not explicitly forbid it.

yes 1

Try it online!

\$\endgroup\$
  • \$\begingroup\$ lmao, and it's true that the rules do not explicitly forbid it. \$\endgroup\$ – LianSheng Jan 4 at 10:00
  • \$\begingroup\$ but it said: Be creative. Avoid simply outputting constant runs of 1s or −1s... \$\endgroup\$ – LianSheng Jan 4 at 10:02
  • 2
    \$\begingroup\$ This answer is perfectly valid, if a bit uncreative @LianSheng. The "avoid outputting constant runs" was merely advice for how to get a higher score (more votes) \$\endgroup\$ – caird coinheringaahing Jan 4 at 16:38
2
\$\begingroup\$

PHP, 127 bytes

$k = base_convert(unpack('H*', "Kaddath")[1], 16, 2);
for(;;$i = $i==strlen($k) ? 0 : $i){
  echo ($k[$i++] ? 1 : -1) . "\n";
}

Try it online!

It first converts the string "Kaddath" to a binary string ("1001011011000010110010001100100011000010111010001101000") then loops on the string to produce the repeated sequence by outputting 1 for each 1 and -1 for each 0

Nothing sensational I fear, just warming up for the new golfing year!

EDIT: clearer code formatting, some parts were golfed by habit

\$\endgroup\$
1
\$\begingroup\$

C (gcc), 14 bytes

n=1;f(){n=-n;}

Try it online!

Next value is returned every time \$f\$ is called.

First answer to repeat alternating signs: \${-1, 1, -1, 1 \dots}\$

\$\endgroup\$
1
\$\begingroup\$

Wolfram Language (Mathematica), 32 bytes

Do[Print@RudinShapiro@n,{n,∞}]

Try it online!

Rudin-Shapiro Sequence

\$\endgroup\$
  • 2
    \$\begingroup\$ Note that "You may not take any input", so just a builtin function is invalid unless it outputs the infinite sequence when called. Changing it to be an infinite loop which calls the builtin each time is allowed \$\endgroup\$ – caird coinheringaahing Jan 2 at 21:54
  • \$\begingroup\$ @cairdcoinheringaahing fixed \$\endgroup\$ – J42161217 Jan 2 at 22:00
1
\$\begingroup\$

R, 33 bytes

Inspired from an earlier code-golf challenge, on the Kempner series, this R code print out 1 when integer contains the digit 9 and -1 otherwise:

while(T<-T+1)show(2*grepl(9,T)-1)

Try it online!

\$\endgroup\$
1
\$\begingroup\$

05AB1E

2[bxS<»,

Try it online!

Repeatedly converts a to binary, replaces 0 with -1 and then set the new a to a in binary interpreted in base-10, starting with a=2. EDIT: Turns out it is A008559

Explaination:

2              Pushes 2
 [             infinite loop
  b            convert to binary (e.g. "10")
   x           pushes tos and itself doubled.
               as strings is 05AB1E are numbers, 10 gets interpreted as a number         
               (e.g. 10, 20)
    S          converts the top of the stack to a string of characters (e.g. ["2", "0"])
     <         subtract 1, which vectorizes.
               because in 05AB1E strings are numbers, it subtract 1 from each of the digits
      »        join by newlines
       ,       print
\$\endgroup\$
1
\$\begingroup\$

Java

My lambda functions below outputs the Kolakoski sequence (A000002), where the 1 and 2 are mapped to -1 and 1 respectively.

The Kolakoski sequence is a self-referential sequence which defines: \$a(n)\$ is the length of the \$n^{th}\$ run, starting at \$a(1)=1\$. Here a visual of the sequence, with the runs underneath it (both lines form the same exact sequence of 1s and 2s):

1 2 2 1 1 2 1 2 2 1 2 2 1 1 2 1 1 2 2 1 2 1 1 2 1 ...
— ——— ——— — — ——— — ——— ——— — ——— ——— — — ——— — —
1  2   2  1 1  2  1  2   2  1  2   2  1 1  2  1 1 ...

Which results in the following lambda function in Java:

import java.util.*;                       // Required import for List/ArrayList/Arrays
()->{                                     // Method without parameter nor return-type:
  List<Integer> sequence = new ArrayList(Arrays.asList(1,2,2));
                                          //  Start the sequence at 1,2,2
  for(int i=2;;i++){                      //  Loop `i` from 2 upwards indefinitely:
    int valueToPrint = sequence.get(i-2); //   Get the `i-2`'th value from the sequence
    System.out.print(valueToPrint*2-3     //   Convert 1 to -1 and 2 to 1, and print it
                      + " ");             //   with space delimiter
    int valueToAdd = i % 2 == 0 ?         //   If `i` is even:
                      1                   //    Create a value 1
                     :                    //   Else (`i` is odd instead):
                      2;                  //    Create a value 2
    sequence.add(valueToAdd);             //   Add that value to the sequence
    if(sequence.get(i) == 2)              //   And if the `i`'th value of the sequence is 2:
      sequence.add(valueToAdd); }}        //    Add that same value again

Try it online.

Golfed this would be 109 bytes:

v->{var s="122";for(int i=1;;s+=(s.charAt(i)>49?11:1)<<i%2)System.out.print((s.charAt(++i-2)-48)*2-3+" ");}

Try it online.

Explanation:

v->{                       // Method with empty unused parameter and no return-type
  var s="122";             //  Sequence-String, starting at 1,2,2
  for(int i=1;             //  Loop from `i` upwards indefinitely:
      ;                    //    After every iteration:
       s+=                 //     Append to the sequence-String:
          (s.charAt(i)>49? //      If the `i`'th digit is a 2:
            11             //       Use 11
           :               //      Else (the `i`'th digit is a 1 instead)
            1)             //       Use a 1
              <<i%2)       //      And bitwise left-shift it by `i` modulo-2
                           //      (1 and 11 are 1 and 1011 in binary respectively.
                           //       Left-shifting this by 0 for even `i` won't change them,
                           //       but left-shifting them by 1 for odd `i` would become
                           //       binary 10 and 10110, which are the integers 2 and 22)
    System.out.print(      //   Print:
      (s.charAt(++i-2)     //    The `i-2`'th character
                           //    (after we've first increased `i` by 1 with `++i`)
       -48)                //    converted from character to integer
           *2-3            //    and transformed from 1/2 to -1/1 respectively
       +" ");}             //    Appended with a space delimiter
\$\endgroup\$
1
\$\begingroup\$

Wolfram Language (Mathematica), 56 bytes

Outputs the sequence infinitely.
Returns 1 if n is a quadratic residue of p=nextPrime(n) and -1 if n is a quadratic nonresidue of p.
In number theory, an integer n is called a quadratic residue modulo p if it is congruent to a perfect square modulo p; i.e., if there exists an integer x such that:

\$ {\displaystyle x^{2}\equiv n{\pmod {p}}} \$

Otherwise, q is called a quadratic nonresidue modulo n.

Do[p=NextPrime[n,1];Print@Mod[n^((p-1)/2),p,-1],{n,∞}]

Try it online!

Let p be an odd prime and gcd(n,p)=1. Then n is a quadratic residue or nonresidue of p according to whether

\$ a^{\frac {p - 1} {2}} \equiv \text {1 (mod p) } \$ or \$ \text{ } a^{\frac {p - 1} {2}} \equiv \text {-1 (mod p)} \$

This is also known as the \$ \text {Legendre symbol (a/b)} \$ and the built-in Mathematica for this is:

JacobiSymbol[n,p]     

Here are the first 100.000 n acuumulated

enter image description here

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 4 bytes

₄Þ·<

Try it online!

Prints [1, -1, -1, -1] repeated forever

₄ push 1000
Þ repeat infinitly - [1, 0, 0, 0, 1, 0, 0, 0, ...]
· double - [2, 0, 0, 0, 2, 0, 0, 0, ...]
< substract 1 - [1, -1, -1, -1, 1, -1, -1, -1, ...]
\$\endgroup\$
0
\$\begingroup\$

PowerShell, 20 bytes

for(){2*!(++$x%3)-1}

Try it online!

1 for divisible by 3, -1 for other

\$\endgroup\$
0
\$\begingroup\$

PowerShell, 68 45 43 bytes

-an enormous 23 bytes thanks to Mazzy! The madlad.

Prints 1 when the integer is a Harshad number (it is divisible by the sum of it's own digits).

for(;){2*!(++$x%($x-replace'','+0'|iex))-1}

Try it online!

Link is to a longer version of the code that terminates after 1000 signs

\$\endgroup\$
  • 1
    \$\begingroup\$ Try it online! :) \$\endgroup\$ – mazzy Jan 6 at 7:01
  • 1
    \$\begingroup\$ @mazzy Wow, really goes to show how bollocks the popular quote Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems. is. Very clever work, as usual! \$\endgroup\$ – Zaelin Goodman Jan 6 at 14:19

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.