mirror of
https://github.com/flancian/garden.git
synced 2026-08-08 07:26:21 +00:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4e9660e18e | |||
| 108a002629 | |||
| 3d4e608b6c | |||
| daba0a76d0 | |||
| f0d717a9a2 | |||
| 59e9af86b1 | |||
| 1bfc15d61e | |||
| e5a5c6dfcd | |||
| 77147c080e | |||
| 530a6c4db3 | |||
| 274e229bdc | |||
| 42bf66b45f |
Executable
+71
@@ -0,0 +1,71 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# https://click.palletsprojects.com/en/8.1.x/arguments/
|
||||||
|
# https://click.palletsprojects.com/en/8.1.x/options/
|
||||||
|
|
||||||
|
import click
|
||||||
|
import math
|
||||||
|
import sys
|
||||||
|
|
||||||
|
# I'm not proud (I am a little bit?).
|
||||||
|
PROOF = [] # :)
|
||||||
|
|
||||||
|
class AgoraCmd(click.Command):
|
||||||
|
def format_help(self, ctx, formatter):
|
||||||
|
click.echo("""Usage:
|
||||||
|
- Visit anagora.org/primes to execute this file in the Agora of Flancia.
|
||||||
|
- Visit e.g. anagora.org/primes/17 to test if 17 is prime.
|
||||||
|
- In general visit anagora.org/foo, anagora.org/foo/bar to execute e.g. <bin/foo.py bar> from your garden.
|
||||||
|
""")
|
||||||
|
|
||||||
|
def __call__(self, *args, **kwargs):
|
||||||
|
try:
|
||||||
|
return super(AgoraCmd, self).__call__(
|
||||||
|
*args, standalone_mode=False, **kwargs)
|
||||||
|
except click.MissingParameter as exc:
|
||||||
|
exc.ctx = None
|
||||||
|
exc.show(file=sys.stdout)
|
||||||
|
click.echo()
|
||||||
|
try:
|
||||||
|
super(AgoraCmd, self).__call__(['--help'])
|
||||||
|
except SystemExit:
|
||||||
|
sys.exit(exc.exit_code)
|
||||||
|
|
||||||
|
@click.command(cls=AgoraCmd)
|
||||||
|
@click.argument('n', type=click.INT)
|
||||||
|
def hexnum(n, centered=True):
|
||||||
|
"""Calculates the first n hex numbers just for fun :)"""
|
||||||
|
|
||||||
|
# Going through this from first principles (from memory/deducing this) for fun :)
|
||||||
|
|
||||||
|
# Why does the first list comprehension I wrote knowing it was wrong not work?
|
||||||
|
# Is it because of its complexity? Note I don't know the 'closed form' for hex numbers yet but I think it exists.
|
||||||
|
# To calculate them, I only know you need to add 6 more than after the previous number.
|
||||||
|
# But that can't be (trivially) expressed in a list comprehension.
|
||||||
|
#
|
||||||
|
# hexnums = [int(centered) + 6*n for n in range(0,n)]
|
||||||
|
# click.echo(f"These are not all hex numbers, but some are: {list(enumerate(hexnums))}")
|
||||||
|
|
||||||
|
# If we could skip one, then two, then three from the list above,
|
||||||
|
# we could exclude all non-hex-numbers.
|
||||||
|
# is there something in itertools that can do this for us?
|
||||||
|
# I don't think so. Maybe dropwhile with the right lambda?
|
||||||
|
|
||||||
|
# ...anyway, going at it old school.
|
||||||
|
# acc = int(centered)
|
||||||
|
# for row in range(0, n+1):
|
||||||
|
# acc += 6 * row
|
||||||
|
# click.echo(f"hex({row}) is {acc}.")
|
||||||
|
|
||||||
|
# ...and then https://oeis.org/A003215 to the rescue :)
|
||||||
|
# [[crystal ball sequence for hexagonal lattice]]: why does this work?
|
||||||
|
# -> https://en.wikipedia.org/wiki/Centered_hexagonal_number
|
||||||
|
# It is a cubic polynomial and Wikipedia shows how to convince yourself that it works.
|
||||||
|
# Beautiful :)
|
||||||
|
hexnums = [3*n*(n+1)+1 for n in range(0,n+1)]
|
||||||
|
|
||||||
|
for nth, hexnum in enumerate(hexnums):
|
||||||
|
click.echo(f"hex({nth}) is {hexnum}.")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
hexnum()
|
||||||
+3
-3
@@ -18,7 +18,7 @@ def print_sieve(sieve):
|
|||||||
primes = []
|
primes = []
|
||||||
for n, prime in enumerate(sieve):
|
for n, prime in enumerate(sieve):
|
||||||
if n >= 2 and prime:
|
if n >= 2 and prime:
|
||||||
primes.append('[[' + str(n) + ']]')
|
primes.append(str(n))
|
||||||
return ", ".join(primes)
|
return ", ".join(primes)
|
||||||
|
|
||||||
def is_prime(n):
|
def is_prime(n):
|
||||||
@@ -73,9 +73,9 @@ def prime(n):
|
|||||||
"""Simple program that factors a number using a [[Sieve of Eratosthenes]]."""
|
"""Simple program that factors a number using a [[Sieve of Eratosthenes]]."""
|
||||||
p, sieve = is_prime(n)
|
p, sieve = is_prime(n)
|
||||||
if p:
|
if p:
|
||||||
click.echo(f"**{n}** is **prime**.")
|
click.echo(f"[[{n}]] is *prime*.")
|
||||||
else:
|
else:
|
||||||
click.echo(f"**{n}** is **not prime**. Want proof? :)")
|
click.echo(f"[[{n}]] is *not prime*. Want proof? :)")
|
||||||
click.echo("\n".join([line for line in PROOF if f'[[{n}]]' in line]))
|
click.echo("\n".join([line for line in PROOF if f'[[{n}]]' in line]))
|
||||||
|
|
||||||
click.echo(f"\nPrimes up to {n}: {print_sieve(sieve)}.")
|
click.echo(f"\nPrimes up to {n}: {print_sieve(sieve)}.")
|
||||||
|
|||||||
@@ -4,3 +4,12 @@
|
|||||||
- I checked with the [[dentist]] and they didn't mind (I tested negative for Covid yesterday), so I went ahead and I'm happy with the results!
|
- I checked with the [[dentist]] and they didn't mind (I tested negative for Covid yesterday), so I went ahead and I'm happy with the results!
|
||||||
- [[Agora]]!
|
- [[Agora]]!
|
||||||
- I thought of implementing ! as a short for #push, meaning that anything wikilinked and "strongly asserted" behaves the same as push: the following blocks are transcluded in the destination.
|
- I thought of implementing ! as a short for #push, meaning that anything wikilinked and "strongly asserted" behaves the same as push: the following blocks are transcluded in the destination.
|
||||||
|
- [[open letters]]!
|
||||||
|
- [[open letter to musk]]
|
||||||
|
- [[carta abierta a milei]]
|
||||||
|
- [[hex]] numbers :)
|
||||||
|
- I calculated [[397]] as the 11th hex number, but if you define them naturally (with one ball yielding the first hex number, not the zeroeth) it would be [[469]]
|
||||||
|
- Still it was fun to do it in my head :) it took 15'-25' while I was doing yoga.
|
||||||
|
- I came away with a renewed appreciation of hex numbers.
|
||||||
|
- I then decided to code an hex [[number]] producer :)
|
||||||
|
- https://github.com/flancian/garden/commit/3d4e608b6cb78aa55b039cd6ce0716e9d8275e9d just for fun.
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
- an [[open letter]].
|
||||||
|
- #go https://docs.google.com/document/d/1RpO8zn2DbG6_pUiLmtk6aoJawZV3UA_cz8Z57jMEubg/edit
|
||||||
|
- #pull [[open letter to elon musk]]
|
||||||
Reference in New Issue
Block a user