Compare commits

12 Commits

Author SHA1 Message Date
flancian 4e9660e18e autopushed 2023-10-17 23:36:22 +02:00
flancian 108a002629 autopushed 2023-10-17 23:33:00 +02:00
flancian 3d4e608b6c Had pure fun writing this :)
First I calculated hex(11) in my head, took about half an hour.

Then I tried to make sense of the sequence the best I could before
looking up the closed form.
2023-10-17 23:24:59 +02:00
flancian daba0a76d0 autopushed 2023-10-17 23:01:10 +02:00
flancian f0d717a9a2 autopushed 2023-10-17 23:00:36 +02:00
flancian 59e9af86b1 autopushed 2023-10-17 22:59:28 +02:00
flancian 1bfc15d61e autopushed 2023-10-17 22:34:50 +02:00
flancian e5a5c6dfcd autopushed 2023-10-17 21:26:47 +02:00
flancian 77147c080e autopushed 2023-10-17 21:26:13 +02:00
flancian 530a6c4db3 autopushed 2023-10-17 21:25:38 +02:00
flancian 274e229bdc autopushed 2023-10-17 20:36:54 +02:00
flancian 42bf66b45f autopushed 2023-10-17 20:35:42 +02:00
4 changed files with 86 additions and 3 deletions
Executable
+71
View File
@@ -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
View File
@@ -18,7 +18,7 @@ def print_sieve(sieve):
primes = []
for n, prime in enumerate(sieve):
if n >= 2 and prime:
primes.append('[[' + str(n) + ']]')
primes.append(str(n))
return ", ".join(primes)
def is_prime(n):
@@ -73,9 +73,9 @@ def prime(n):
"""Simple program that factors a number using a [[Sieve of Eratosthenes]]."""
p, sieve = is_prime(n)
if p:
click.echo(f"**{n}** is **prime**.")
click.echo(f"[[{n}]] is *prime*.")
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(f"\nPrimes up to {n}: {print_sieve(sieve)}.")
+9
View File
@@ -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!
- [[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.
- [[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.
+3
View File
@@ -0,0 +1,3 @@
- an [[open letter]].
- #go https://docs.google.com/document/d/1RpO8zn2DbG6_pUiLmtk6aoJawZV3UA_cz8Z57jMEubg/edit
- #pull [[open letter to elon musk]]