# The "Fly straight, dammit" sequence

## Background

"Fly straight, dammit" (OEIS A133058) is a sequence of integers, which has these rules:

• $$\a_0 = a_1 = 1\$$

• $$\a_n = a_{n-1}+n+1\$$ if $$\gcd(a_{n-1}, n) = 1\$$

• Otherwise, $$\a_n = \frac{a_{n-1}}{gcd(a_{n-1}, n)}\$$

In pseudocode:

a[0] = a[1] = 1
if gcd(a[n-1], n) == 1 then
a[n] = a[n-1] + n + 1
else
a[n] = a[n-1] / gcd(a[n-1], n)


(GCD stands for "Greatest Common Divisor". For example, GCD(8, 12) = 4.)

The graph for this function looks like this:

After n=638, it goes into four straight lines ($$\y=1\$$ and $$\y=2\$$ are indistinguishable in this picture, but they are separate lines).

'Whenever I look at this sequence I am reminded of the great "Fly straight, dammit" scene in the movie "Avatar".' - N. J. A. Sloane

## Your task

You have to:

• accept an input, n, from the user (it will be a positive integer)
• calculate $$\a_n\$$ and output (this is so any output is fine)

This is so shortest answer in bytes wins!

• Please consider adding test cases in your challenges. Also avoid accepting an answer in the first day of the challenge. Finally, please use "sandbox" that you will find in the upper right corner of the site. Welcome to code golf! Sep 5 at 17:33
• I'll have to add this to my list of examples where extrapolation badly fails...
– TLW
Sep 6 at 6:06
• That's actually four straight lines at the end: $y=1$, $y=n+2$, $y=2(n+1)$, and $y=2$. Sep 8 at 15:36
• @Nitrodon - thanks. I couldn't see the difference between $y=1$ and $y=2$ at that zoom. I have edited the question with this information. Sep 8 at 15:37

# 05AB1E, 12 bytes

1‚λDN¿DiNOë÷


Outputs the infinite sequence.

Try it online.

Explanation:

1           # Push 1
‚          # Pair the top two values together. Since there is only one value on the
# stack and there is no input, it implicitly uses the last value that was
# on the stack, resulting in [1,1]

λ         # Start a recursive environment,
# to output the infinite sequence
# (which is output implicitly afterwards)
1‚          # Start with a(0)=a(1)=1
# Where every following a(n) is calculated as:
#  (implicitly push a(n-1) to the stack)
D        #  Duplicate a(n-1)
N       #  Push n
¿      #  Pop one a(n-1) and n, and calculate their GCD
D     #  Duplicate this gcd(a(n-1),n)
i    #  If it's 1:
N   #   Push n again
O  #   Sum the three values on the stack: a(n-1)+gcd(a(n-1),n)+n,
#   where gcd(a(n-1) is always 1 in this case
ë    #  Else:
÷   #   Integer-divide a(n-1) by gcd(a(n-1),n)

• Shouldn't that be '12 UTF8 unicode characters'?
– P i
Sep 8 at 13:07
• @Pi 05AB1E uses, like some other code-golfing languages you'll see pretty often (like Vyxal, Jelly, Charcoal, MathGolf, etc.) it's own custom codepage for the 256 characters it knows. I usually link them in the bytes portion of my answers, as you can see above. Each of these 256 characters are 1 byte each in this 05AB1E encoding. I'm not sure how to compile/run it with raw bytes in the new 05AB1E version that's built in Elixir, but here is an example of the legacy 05AB1E version, which was built in Python, where the raw bytes are run with --osabie flag. Sep 8 at 13:32
• I was curious from an information-theory perspective. So each glyph is holding 8 bits of information. Fascinating. Props for patterning your brain with this.
– P i
Sep 8 at 16:07

# C (GCC), 65 bytes

t,r;h(a,b){r=b?h(b,a%b):a;}c(n){n=n<2?:h(t=c(n-1),n)<2?t-~n:t/r;}


Attempt This Online!

This is based on py3programmer's answer; I'm posting this mostly for their benefit since they say they didn't fully understand the golfing suggestions I gave.

Here's their current version:

h(a,b){if(b){return h(b,a%b);}else{return a;}}c(n){if(n<2){return 1;}if(h(c(n-1),n)<2){return c(n-1)+n+1;}else{return c(n-1)/h(c(n-1),n);}}


The first thing I did was convert the if/else statements into ?/: conditional expressions. You can read x?y:z as "if x then y, otherwise z".

h(a,b){return b?h(b,a%b):a;}c(n){return n<2?1:h(c(n-1),n)<2?c(n-1)+n+1:c(n-1)/h(c(n-1),n);}


GCC has a non-standard extension to C that lets you use x?:z to mean "if x, then x, otherwise z":

h(a,b){return b?h(b,a%b):a;}c(n){return n<2?:h(c(n-1),n)<2?c(n-1)+n+1:c(n-1)/h(c(n-1),n);}


Another quirk of GCC, when used in -O0 optimisation mode (which is the default), is that the CPU register used for the return value of a function is the same as the one used for its first parameter. As a result, we can replace return  statements with assignments to the first parameter for both functions:

h(a,b){a=b?h(b,a%b):a;}c(n){n=n<2?:h(c(n-1),n)<2?c(n-1)+n+1:c(n-1)/h(c(n-1),n);}


Then I noticed that the value c(n-1) is reused quite a lot, so I extracted it into a temporary variable:

t;h(a,b){a=b?h(b,a%b):a;}c(n){n=n<2?:h(t=c(n-1),n)<2?t+n+1:t/h(t,n);}


I declared it as a global variable, because that doesn't require a type in the declaration, but it makes no difference in functionality.

Then I made use of this tip which states that x+y+1 can be shortened to x-~y (because ~y is equivalent to -y - 1)

t;h(a,b){a=b?h(b,a%b):a;}c(n){n=n<2?:h(t=c(n-1),n)<2?t-~n:t/h(t,n);}


I also noticed that h(t,n) is reused, so I modified h to store its result in a global variable r as well as providing it as a return value. We can then use r instead of calling h again:

t,r;h(a,b){r=a=b?h(b,a%b):a;}c(n){n=n<2?:h(t=c(n-1),n)<2?t-~n:t/r;}


As pointed out by Kevin Cruijssen in a comment, the a= assignment turns out to be unnecessary. Why, exactly, is beyond me, but it seems GCC just stores the value in the return register by happenstance.

t,r;h(a,b){r=b?h(b,a%b):a;}c(n){n=n<2?:h(t=c(n-1),n)<2?t-~n:t/r;}

• Since you save the result of h in global variable r, it no longer needs to 'return', so you can remove the a= within h to save 2 bytes. Sep 5 at 15:26
• @KevinCruijssen Huh, I don't understand that. Why does the return value of calling h become right, even though I only put it into a global variable? Sep 5 at 15:31
• I was actually trying to figure that out myself after I made my comment just yet and realizing it would still need a return for h(t=c(n-1),n)<2.. 🤔 The result didn't seem to change, hence my comment, but it's indeed a bit confusing. I'm trying to find a difference with or without the a= for a certain n, but there doesn't seem to be one for the values I've checked thus far. Although I'm not sure why.. Sep 5 at 15:34
• @KevinCruijssen Well, they generate identical assembly: godbolt.org/z/EaMK6KEWd Sep 5 at 15:38
• Oh sorry.. If you mean why an external (global) variable is good for the eax trick Idk for sure, i think it's just the last assignment the cpu perform but I know for sure it works consistently in gcc Sep 5 at 19:26

# Prolog (SWI), 68 56 bytes

-12 bytes thanks to @Steffan

N+X:-N>1,N-1+B,C=gcd(B,N),(C<2,X is B+N+1;X is B/C);X=1.


Try it online!

• 56: N+X:-N>1,N-1+B,C=gcd(B,N),(C<2,X is B+N+1;X is B/C);X=1. Sep 5 at 19:34
• @Steffan Yo how is that not hitting stack limit, I tried N-1+B and it was always hitting stack limit for some reason. Sep 5 at 19:35
• That's because you're checking directly if N=1 or N=0 with the top checks and not evaluating N-1. I'm using N>1 which evaluates it. Sep 5 at 19:37

# C (gcc), 234 210 143 139 bytes

Thanks to pxeger for removing a few bytes. Check out his improved answer.

h(a,b){if(b){return h(b,a%b);}else{return a;}}c(n){if(n<2){return 1;}if(h(c(n-1),n)<2){return c(n-1)+n+1;}else{return c(n-1)/h(c(n-1),n);}}

• You can remove most of the int  bits from the declarations, as C variables default to type int if none is specified. Sep 5 at 14:30
• From the definitions, the variables, or the arguments? Sep 5 at 14:31
• From all of them. Sep 5 at 14:32
• You also don't need to submit a full program, so you can remove the main() function entirely and take I/O using the arguments and return value of c Sep 5 at 14:33
• I've done that in a few other answers, but because of the first line and the other definitions, I'm not so sure it's feasible to do that. Could you edit it yourself and show? Sep 5 at 14:36

# JavaScript (ES6),  55  53 bytes

f=(n,a=n,b=q=n>1?f(n-1):-1)=>b?f(n,b,a%b):q/a-~n*!~-a


Try it online!

### Commented

f = (                // f is a recursive function taking:
n,                 //   n = input
a = n,             //   a = 1st argument of the GCD, initialized to n
b = q =            //   b = 2nd argument of the GCD, initialized to ...
n > 1 ? f(n - 1) //       q = f(n - 1) if n > 1
: -1       //       q = -1 if n ≤ 1 (which will force the GCD
) =>                 //           to be evaluated as -1 as well)
b ?                // if b is not equal to 0:
f(n, b, a % b)   //   keep computing the GCD with a recursive call
:                  // else:
//   the GCD is now loaded in a
q / a -          //   return q / a
~n * !~-a        //   add n + 1 if a = 1
//   (if q was set to -1, we get -1/-1-~n*0 = 1)


# Wolfram Language (Mathematica), 47 bytes

If[#<2,1,If[(s=GCD[#,d=#0[#-1]])<2,d+#+1,d/s]]&


Try it online!

-4 bytes from alephalpha

• If[#<2,1,If[(s=GCD[#,d=#0[#-1]])<2,d+#+1,d/s]]& Sep 6 at 8:22

# Python, 68 bytes

import math
f=lambda n:n<2or(a:=f(n-1))//(q:=math.gcd(a,n))-~n*(q<2)


Attempt This Online!

• I guess this would be Python 3.8 or more (because of :=)? Sep 5 at 14:30
• @py3programmer Yes, I use the latest version of Python as much as possible. Sep 5 at 14:31

# Python 3, 115 109 bytes

from math import*
def f(n):
l=1
for i in range(2,n+1):
g=gcd(l,i)
if g==1:l+=i+1
else:l//=g
print(l)


Try it online!

Ungolfed:

from math import gcd                               # Function for getting gcd of two numbers
def fsd_sequence(n):                               # Define function to get the nth value of the sequence
prev = 1                                       # The previous value (a[n-1])
for i in range(2, n + 1):                      # Iterate through numbers from 2 to n+1
greatest_common_divisor = gcd(prev, i)     # GCD of previous value and i
if greatest_common_divisor == 1:           # If GCD equals 1,
prev = prev + i + 1                    # Increment the previous value by i+1
else:                                      # Otherwise
prev = prev // greatest_common_divisor # Set the previous value to itself divided by the GCD
return l                                       # Return the last value

• You can put l=1 inside the function definition: def f(n,l=1); if g==1:l+=i+1\n else:l//=g can be l=g==1and l-~i or l//g; g=gcd(l,i) can be placed inline as (g:=gcd(l,i)) in Python 3.8+; and the loop can be inline then as well: 99 bytes I'm also pretty sure it can be a recursive lambda to get rid of the spaces and newlines, but haven't looked too closely. Sep 5 at 14:29
• Ungolfed Sage plot: sagecell.sagemath.org/… Sep 6 at 12:45
• It would be smaller to just import math and then use math.gcd later, since you only use it once. It's only 1 byte, but every byte counts Sep 6 at 21:12
• is it shorter to implement gcd manually? something like while i:i,g=g,i%g
– qwr
Sep 7 at 6:37

# Vyxal, 15 bytes

λ1>[‹x~ġ:ċ[ḭ|++


Try it Online!

# PARI/GP, 44 bytes

f(n)=if(n<2,1,2>s=gcd(n,d=f(n-1)),d+n+1,d/s)


Attempt This Online!

# Ruby, 59 ... 48 bytes

->n{k=z=1;3.upto(n){|v|z=v.gcd k=z>1?k/z:k+v};k}


Try it online!

# Raku, 46 bytes

1,1,{$_/($!=(++$+1)gcd$_)+(++$+2)*($!==1)}...*


Try it online!

This is an expression for the infinite sequence.

• 42 bytes, though it relies on not messing with $/ while the sequence is being generated (though you can still save one regardless with $!<2)
– Jo King
Sep 13 at 1:57

# Japt, 17 bytes

Outputs the first n terms. I feel like I'm missing a trick here to save a byte or 2.

ÈjY ?XÒY:X/yY}hBì


Try it

ÈjY ?XÒY:X/yY}hBì     :Implicit input of integer U
È                     :Function taking an integer X and an index Y as arguments
jY                   :  Are X & Y co-prime?
?                 :  If so then return
XÒY              :    X-~Y = X+Y+1
:             :  Else return
X/           :    X divided by
yY         :    GCD of X & Y
}        :End function
h       :Repeatedly take the last element of the following array, pass it through the function and push it back to the array until it reaches length U
B      :  11
ì     :  To digit array
:Implicit output of resulting array


# PowerShell Core, 111 bytes

for($i=$r=1;$i-lt"$args"){for($a,$b=++$i,$r;$b){if($a-gt$b){$b,$a=$a,$b}$b%=$a}$r=(($r/$a),($r+$i+1))[!--$a]}$r


Try it online!

Outputs the $$\n^{th}\$$ term of the sequence

All the terms for 102 bytes