Skip to content

Instantly share code, notes, and snippets.

View JJ's full-sized avatar
🏠
Working from home

Juan Julián Merelo Guervós JJ

🏠
Working from home
View GitHub Profile
@JJ
JJ / Fibonacci.pm
Created December 23, 2016 18:37 — forked from massa/Fibonacci.pm
Fibonacci sequence implementation in Perl6
my constant @fib = 1, 1, *+* ... *;
@JJ
JJ / Madhava-Leibniz-series.sh
Last active January 6, 2017 11:29
Cool perl6 one liners vol. 2: Computing Pi from a series
#You can also copy/paste this directly into the command line if perl6 is installed
perl6 -e "say π - 4 * ([+] (( 1 <</<< (1,5,9...5000) ) Z ( -1 <</<< (3,7...5000))).flat)"
# This is an implementation of the Madhava-Lebniz series: https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80
# This script creates two series (1,5...) and (3,7) for even and odd positions in the sequence.
# Then, the inverse of the sequence is computed 1/(the sequence) with the 1<</<< construction
# Then they are zipped together using Z
# Finally, [+] sums all terms of the sequence together. Since the sequence is an approximation to π/4 it's multiplied by 4
# and then substracted from π. This should give a good 3-digit approximation. Change 5000 by a bigger number if you want bigger precision
@JJ
JJ / Madhava-Leibniz-series-v2.sh
Last active March 22, 2021 08:11
Cool Perl 6 one liners vol. 2: computing Pi in a series
#You can also copy/paste this directly into the command line if perl6 is installed
raku -e "say π - 4 * ([+] <1 -1> <</<< (1,3,5,7,9...10000)) "
# This is an implementation of the Madhava-Lebniz series: https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80
# And a better, or at least shorter, version of https://gist.github.com/JJ/23801c82b218b25da7fc8831b7c331ad
# Instead of creating two series and combining them, this combines 1,-1 and the series of odd numbers
# by applying them successively: 1 divided by the first, -1 by the second, and so on. This creates exactly the same effect.
# It is faster and you only need to change one number, the 10000, to compute Pi with a bigger precision.
@JJ
JJ / dumb-cypher.sh
Created January 6, 2017 17:38
Perl6 one liners vol. 3: a very simple cypher
# This cypher applies character displacement to a set of letters using a "cypher" both users must have
perl6 -e "say (<1 -1 3 -3 4> <<+<< <t h i s i s s o s 1 k r i t>.map: {.ord}).map: {.chr}"
# In this case, the cypher is <1 -1 3 -3 4> and will be applied to the message after <<+<<
# You can retrieve the original message by applying the inverse of the cypher
perl6 -e "say (<-1 1 -3 3 -4> <<+<< <u g l p m t r r p 5 l q l q>.map: {.ord}).map: {.chr}"
@JJ
JJ / binomial-coefficient.sh
Created January 15, 2017 19:01
Perl6 one liners, vol 3: Print n over m, or the binomial coefficient for n and m
# The first argument will be n and entered into the $^ß placeholder, the second into the $^þ placeholder
perl6 -e 'say { ([*] 1..$^ß ) / ( [*] 1..$^þ) * ([*] 1..($^ß - $^þ)) }(@*ARGS[0],@*ARGS[1])' 20 11
# [*] 1..$^ß ) will compute the factorial by creating a range 1..$^ß and then multiplying all of them together.
# @*ARGS contains the arguments handled to the script; @*ARGS[0] will be the first and so on.
@JJ
JJ / backup.mk
Last active March 18, 2017 08:08
Backup using Make
.PHONY: clean
BKDIR = /tmp/copia
$(BKDIR): .
rsync -avz $< $@
clean:
rm *~
@JJ
JJ / zip.mk
Created March 18, 2017 08:08
zip via Make
.PHONY: clean
recipes.zip: .
zip recipes.zip *.mk *.md
clean:
rm *~
@JJ
JJ / hunspell.mk
Created March 18, 2017 08:10
Spell check via makefile
.PHONY: spellcheck
spellcheck:
hunspell -p words.dic -l *.md | grep . && exit 2 || exit 0
@JJ
JJ / spell-n-commit.mk
Created March 18, 2017 08:11
Conditional commit if spelling is correct
.PHONY = commit
status:
-git status -s | grep . && touch status
commit: status
rm status; make -f 3.mk
git commit -am "$(msg)"
@JJ
JJ / latex.mk
Created March 18, 2017 08:12
Generate latex file
%.pdf: %.tex
pdflatex $<
bibtex $(basename $<)
pdflatex $<
pdflatex $<