Menu
Newbedev LogoNEWBEDEV Python Javascript Linux Cheat sheet
Newbedev LogoNEWBEDEV
  • Python 1
  • Javascript
  • Linux
  • Cheat sheet
  • Contact

Two steps forward and one step back

Python 2, 18 bytes

lambda n:3*n-1%n*4

Try it online.

I picked this trick up from xnor just a few days ago…!


Python 2, 20 bytes

lambda n:n*3-4*(n>1)

Try it online!


Python 2, 17 bytes

lambda n:n-3%~n*2

Try it online!

I found the expression by brute-force search. It effectively computes n+2*abs(n-2).

Tags:

Math

Code Golf

Arithmetic

Related

Ten-pin bowling score - World Bowling edition Running gene crossover algorithm Eiffel Towers: Create a large "A" from "A"s Implement Lazy Drop Sort Alice and Bob have a fight Factory workers The next colour Blue duck, red duck, gray duck Cross-Alphabetic Characters Horizontally Aligned ASCII Art Self-Validating Triangular Checkerboard Program N times program for the N-th number

Recent Posts

Pandas how to find column contains a certain value Recommended way to install multiple Python versions on Ubuntu 20.04 Build super fast web scraper with Python x100 than BeautifulSoup How to convert a SQL query result to a Pandas DataFrame in Python How to write a Pandas DataFrame to a .csv file in Python
© 2021 newbedevPrivacy Policy