Skip to main content
Commonmark migration
Source Link

#Python 3.8, 104 74 65 60 57 bytes

Python 3.8, 104 74 65 60 57 bytes

lambda n:(-2,6)[n>4*(x:=(n**.5+1)//2)*x+2*x]*x+2+~n+8*x*x

Edit: Thanks to Johnathan Allan for getting it from 74 to 57 bytes!

This solution uses 0-based indexing.

#Python 3.8, 104 74 65 60 57 bytes

lambda n:(-2,6)[n>4*(x:=(n**.5+1)//2)*x+2*x]*x+2+~n+8*x*x

Edit: Thanks to Johnathan Allan for getting it from 74 to 57 bytes!

This solution uses 0-based indexing.

Python 3.8, 104 74 65 60 57 bytes

lambda n:(-2,6)[n>4*(x:=(n**.5+1)//2)*x+2*x]*x+2+~n+8*x*x

Edit: Thanks to Johnathan Allan for getting it from 74 to 57 bytes!

This solution uses 0-based indexing.

added 7 characters in body
Source Link
Kapocsi
  • 221
  • 1
  • 4

#Python 3.8, 104 74 6565 60 57 bytes

deflambda f(n):x=((n-12,6)**[n>4*(x:=(n**.5+1)//2;return(-2,6)[n>4*x*x+2*x]*x+2-n+8*x*x*x+2*x]*x+2+~n+8*x*x

Edit: Thanks to Johnathan Allan for getting it from 74 to 6557 bytes!

This solution uses 0-based indexing.

#Python 3, 104 74 65 bytes

def f(n):x=((n-1)**.5+1)//2;return(-2,6)[n>4*x*x+2*x]*x+2-n+8*x*x

Edit: Thanks to Johnathan Allan for getting it from 74 to 65 bytes!

#Python 3.8, 104 74 65 60 57 bytes

lambda n:(-2,6)[n>4*(x:=(n**.5+1)//2)*x+2*x]*x+2+~n+8*x*x

Edit: Thanks to Johnathan Allan for getting it from 74 to 57 bytes!

This solution uses 0-based indexing.

added 67 characters in body
Source Link
Kapocsi
  • 221
  • 1
  • 4

#Python 3, 104 7474 65 bytes

def af(n):x=(int((n-1)**.5)+15+1)//2;return 8*x**2+(6,-2,6)[n<=4*x**2+2*x]*x+2[n>4*x*x+2*x]*x+2-nn+8*x*x

Edit: Thanks to Johnathan Allan for getting it from 74 to 65 bytes!

#Python 3, 104 74 bytes

def a(n):x=(int((n-1)**.5)+1)//2;return 8*x**2+(6,-2)[n<=4*x**2+2*x]*x+2-n

#Python 3, 104 74 65 bytes

def f(n):x=((n-1)**.5+1)//2;return(-2,6)[n>4*x*x+2*x]*x+2-n+8*x*x

Edit: Thanks to Johnathan Allan for getting it from 74 to 65 bytes!

added 26 characters in body
Source Link
Jonathan Allan
  • 110.1k
  • 7
  • 65
  • 282
Loading
fixed byte count
Source Link
Kapocsi
  • 221
  • 1
  • 4
Loading
golfed more
Source Link
Kapocsi
  • 221
  • 1
  • 4
Loading
added 1 character in body
Source Link
Shaggy
  • 43.5k
  • 4
  • 36
  • 94
Loading
deleted 3 characters in body
Source Link
Kapocsi
  • 221
  • 1
  • 4
Loading
Source Link
Kapocsi
  • 221
  • 1
  • 4
Loading