Show me a Random Blog Post


folding paper folding tube maps london underground platonic solids rhombicuboctahedron raspberry pi weather station programming python php news royal baby probability game show probability christmas flexagons frobel coins reuleaux polygons countdown football world cup sport stickers tennis braiding craft wool emf camp people maths trigonometry logic propositional calculus twitter mathslogicbot oeis pac-man graph theory video games games chalkdust magazine menace machine learning javascript martin gardner reddit national lottery rugby puzzles advent game of life dragon curves fractals pythagoras geometry triangles european cup dates palindromes chalkdust christmas card bubble bobble asteroids final fantasy curvature binary arithmetic bodmas statistics error bars estimation accuracy


Show me a Random Blog Post
▼ show ▼
 2015-08-29 08:48:00 

How OEISbot Works

A few weeks ago, I made OEISbot, a Reddit bot which posts information whenever an OEIS sequence is mentioned.
This post explaind how OEISbot works. The full code can be found on GitHub.

Getting Started

OEISbot is made in Python using PRAW (Python Reddit Api Wrapper). PRAW can be installed with:
pip install praw
Before making a bot, you will need to make a Reddit account for your bot, create a Reddit app and obtain API keys. This python script can be used to obtain the necessary keys.
Once you have your API keys saved in your praw.ini file, you are ready to make a bot.

Writing the Bot

First, the necessary imports are made.
import praw
import re
import urllib
import json
from praw.objects import MoreComments
To prevent OEISbot from posting multiple links to the same sequence in a thread, a list of all the sequences which have been linked to is loaded.
with open("/home/pi/OEIS/seen") as f:
    seen = json.load(f)
Next, OEISbot logs into Reddit.
r = praw.Reddit('OEIS link and description poster by /u/mscroggs.')

access_i = r.refresh_access_information(refresh_token=r.refresh_token)

auth = r.get_me()
The subs which OEISbot will search through are listed. I have used all the math(s) subs which I know about, as these will be the ones mentioning sequences.
subs = ['math','mathpuzzles','casualmath','theydidthemath',
The markup function loads the necessary information from OEIS and formats it. Each comment will end with the output of the me function. The ouput of joiner will be used between sequences which are mentioned.
def markup(seq_n):
    pattern = re.compile("%N (.*?)<",re.DOTALL|re.M)
    pattern = re.compile("%S (.*?)<",re.DOTALL|re.M)
    new_com = "[A"+seq_n+"]("+seq_n+"/): "
    new_com += desc+"\n\n"
    new_com += seq+"..."
    return new_com

def me():
    return "I am OEISbot. I was programmed by /u/mscroggs. [How I work]("

def joiner():
    return "\n\n- - - -\n\n"
For each sub OEISbot is monitoring, the hottest 10 posts are searched through for mentions of sequences. If a mention is found, a reply is generated and posted.
for sub in subs:
    subreddit = r.get_subreddit(sub)
    for submission in subreddit.get_hot(limit = 10):
        except KeyError:
            seen[] = []
        re_s = re.findall("A([0-9]{6})",submission.title)
        re_s += re.findall("oeis\.org/A([0-9]{6})",submission.url)
        post_me = []
        for seq_n in re_s:
            if seq_n not in seen[]:
        if len(post_me)>0:
        flat_comments = praw.helpers.flatten_tree(submission.comments)
        for comment in flat_comments:
            if not isinstance(comment,MoreComments) and is not None and != "OEISbot":
                re_s = re.findall("A([0-9]{6})",comment.body)
                post_me = []
                for seq_n in re_s:
                    if seq_n not in seen[]:
                if len(post_me)>0:
The list of sequences which have been posted in each thread is saved to prevent duplication later.
with open("/home/pi/OEIS/seen","w") as f:

Running the Code

I put this script on a Raspberry Pi which runs it every 10 minutes (to prevent OEISbot from getting refusals for posting too often). This is achieved with a cron job.
*/10 * * * * python /path/to/

Making Your Own Bot

The full OEISbot code is available on GitHub. Feel free to use it as a starting point to make your own bot! If your bot is successful, let me know about it in the comments below or on Twitter.

Similar Posts

Logical Contradictions
Logic Bot, pt. 2
Logic Bot
Raspberry Pi Weather Station


Comments in green were written by me. Comments in blue were not written by me.
 Add a Comment 

I will only use your email address to reply to your comment (if a reply is needed).

To prove you are not a spam bot, please type "w" then "e" then "i" then "r" then "d" in the box below (case sensitive):
 2014-11-26 15:08:48 

Logic Bot

Last week, mathslogicbot started the long task of tweeting every tautology in propositional calculus. This post explains what this means and how I did it.

What is Propositional Calculus?

Propositional calculus is a form of mathematical logic, in which the formulae (the logical 'sentences') are made up of the following symbols:


Formulae are defined recursively using the following rules:
For example, \((a\vee b)\), \(\neg f\) and \(((a\vee b)\rightarrow\neg f)\) are formulae.
Each of the variables is assigned a value of either "true" or "false", which leads to each formula being either true or false:


A tautology is a formula that is true for any assigment of truth values to the variables. For example:
\((a\vee \neg a)\) is a tautology because: if \(a\) is true then \(a\) or \(\neg a\) is true; and if \(a\) is false, then \(\neg a\) is true, so \(a\) or \(\neg a\) is true.
\((a\leftrightarrow a)\) is a tautology because: if \(a\) is true then \(a\) and \(a\) are both true; and if \(a\) is false then \(a\) and \(a\) are both false.
\((a\wedge b)\) is not a tautology because if \(a\) is true and \(b\) is false, then it is false.
The following are a few more tautologies. Can you explain why they are always true?


If you want to play with the Logic Bot code, you can download it here.
In order to find all tautologies less than 140 characters long, one method is to first generate all formulae less than 140 characters then check to see if they are tautologies. (This is almost certainly not the fastest way to do this, but as long as it generates tautolgies faster than I want to tweet them, it doesn't matter how fast it runs.) I am doing this on a Raspberry Pi using Python in the following way.

All Formulae

The following code is writing all the formulae that are less than 140 characters to a file called formulae.
from os.path import join
First import any modules needed and set the path where the file will be saved.
def candidate(formula):
    global formulae

    if len(formula)<=140 and formula not in formulae:
        print formula
This function checks that a formula is not already in my list of formulae and shorter than 140 characters, then adds it to the list and writes it into the file.
variables=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "@", "#", "2", "3", "4", "5", "6", "7", "8", "9"]
This line says which characters are going to be used as variables. It is impossible to write a formula in less that 140 characters with more than 36 different variables so these will be sufficient. I haven't used 0 and 1 as these are used to represent false and true later.
for i in range(0,len(formulae)):
These lines load the formulae already found from the file. This is needed if I have to stop the code then want to continue.

while oldlen!=newlen:
    for f in formulae+variables:
    for f in formulae+variables:
        for g in formulae:
            for star in ["I", "F", "N", "U"]:
The code inside the while loop goes through every formula already found and puts "-" in front of it, then takes every pair of formulae already found and puts "I", "F", "N" or "U" between them. These characters are used instead of the logical symbols as using the unicode characters leads to numerous python errors. The candidate function as defined above then adds them to the list (if they are suitable). This continues until the loop does not make the list of formulae longer as this will occur when all formulae are found.
Once the loop has finished this will add the string "#FINISHED#" to the file. This will tell the truth-checking code when the it has checked all the formulae (opposed to having checked all those generated so far).


Now that the above code is finding all formulae, I need to test which of these are tautologies. This can be done by checking whether every assignment of truth values to the variables will lead to the statement being true.
from os.path import join
First import any modules needed and set the path where the file will be saved.
def next(ar,i=0):
    global cont
    if i        if ar[i]=="0":
    return ar
Given an assignment of truth values, this function will return the next assignment, setting cont to False if all the assignments have been tried.
def solve(lo):
    lo=lo.replace("-0", "1")
    lo=lo.replace("-1", "0")

    lo=lo.replace("(0I0)", "1")
    lo=lo.replace("(0I1)", "1")
    lo=lo.replace("(1I0)", "0")
    lo=lo.replace("(1I1)", "1")

    lo=lo.replace("(0F0)", "1")
    lo=lo.replace("(0F1)", "0")
    lo=lo.replace("(1F0)", "0")
    lo=lo.replace("(1F1)", "1")

    lo=lo.replace("(0N0)", "0")
    lo=lo.replace("(0N1)", "0")
    lo=lo.replace("(1N0)", "0")
    lo=lo.replace("(1N1)", "1")

    lo=lo.replace("(0U0)", "0")
    lo=lo.replace("(0U1)", "1")
    lo=lo.replace("(1U0)", "1")
    lo=lo.replace("(1U1)", "1")

    return lo
This function will replace all instances of "NOT TRUE" with "FALSE" and so on. It will be called repeatedly until a formula is reduced to true or false.


variables=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "@", "#", "2", "3", "4", "5", "6", "7", "8", "9"]
These lines read the formulae from the file they are saved in and load how many have been checked if this script has been restarted. The the variables are set.
while formulae[-1]!="#FINISHED#" or i  if i    formula=formulae[i].strip("\n")
These lines will loop through all formulae until "#FINISHED#" is reached.
    for a in variables:
        if a not in formula:
        elif not insofar:
Here, the code checks that if a variable is in the formula, then all the previous variables are in the formula. This will prevent the Twitter bot from repeating many tautologies that are the same except for the variable a being replaced by b (although there will still be some repeats like this. Can you work out what these will be?).
    if not fail:
        while cont and taut:
            for j in range(0,len(inA)):
            while feval not in ["0", "1"]:
            if feval!="1":
        if taut:

Now, the formula is tested to see if it is true for every assignment of truth values. If it is, it is added to the file containing tautologies. Then the number of formulae that have been checked is written to a file (in case the script is stopped then resumed).
If the end of the formulae file is reached, then the file is re-loaded to include all the formulae found while this code was running.


Finally, I wrote a code that tweets the next item in the file full of tautologies every three hours (after replacing the characters with the correct unicode characters).

How Long Will it Take?

Now that the bot is running, it is natural to ask how long it will take to tweet all the tautologies.
While it is possible to calculate the number of formulae with 140 characters or less, there is no way to predict how many of these will be tautologies without checking. However, the bot currently has over 13 years of tweets lines up. And all the tautologies so far are under 30 characters so there are a lot more to come...
Edit: Updated time left to tweet.

Similar Posts

Logical Contradictions
Logic Bot, pt. 2
How OEISbot Works
Raspberry Pi Weather Station


Comments in green were written by me. Comments in blue were not written by me.
 Add a Comment 

I will only use your email address to reply to your comment (if a reply is needed).

To prove you are not a spam bot, please type "etogyx" backwards in the box below (case sensitive):
 2013-07-11 20:39:00 

Raspberry Pi Weather Station

On 19 June, the USB temperature sensor I ordered from Amazon arrived. This sensor is now hooked up to my Raspberry Pi, which is taking the temperature every 10 minutes, drawing graphs, then uploading them here. Here is a brief outline of how I set this up:

Reading the Temperature

I found this code and adapted it to write the date, time and temperature to a text file. I then set cron to run this every 10 minutes. It writes the data to a text file (/var/www/temperature2) in this format:
2013 06 20 03 50,16.445019
2013 06 20 04 00,16.187843
2013 06 20 04 10,16.187843
2013 06 20 04 20,16.187843

Plotting the Graphs

I found a guide somewhere on the internet about how to draw graphs with Python using Pylab/Matplotlib. If you have any idea where this could be, comment below and I'll put a link here.
In the end my code looked like this:
import time
import matplotlib as mpl
import matplotlib.pylab as plt
import matplotlib.dates as mdates

ts = time.time()
import datetime
st = mdates.date2num(datetime.datetime(int(float(now.strftime('%Y'))), int(float(now.strftime('%m'))), int(float(now.strftime('%d'))), 0, 0, 0))
weekst = st-int(float(datetime.datetime.fromtimestamp(ts).strftime('%w')))

for line in f:
  fL[0]=fL[0].split(" ")
  if cur==-1:
  if datC>=st and datC<=st+1:
  if datC>=st-1 and datC<=st:
  if datC>=weekst and datC<=weekst+7:
  if datC>=weekst-7 and datC<=weekst:
  if datC>cur+1:

plt.ylabel("Temperature ($^\circ$C)")
plt.legend(['today','yesterday'], loc='upper left',prop={'size':8})
labels = plt.gca().get_xticklabels()


plt.ylabel("Temperature ($^\circ$C)")
plt.legend(['this week','last week'], loc='upper left',prop={'size':8})
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('                     %A'))
labels = plt.gca().get_xticklabels()


plt.xlabel("Date & Time")
plt.ylabel("Temperature ($^\circ$C)")
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%y %H:%M'))
labels = plt.gca().get_xticklabels()


plt.ylabel("Temperature ($^\circ$C)")
plt.legend(['minimum','maximum'], loc='upper left',prop={'size':8})
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d %b'))
labels = plt.gca().get_xticklabels()
If there's anything in there you don't understand, comment below and I'll try to fill in the gaps.

Uploading the Graphs

Finally, I upload the graphs to To do this, I set up pre-shared keys on the Raspberry Pi and this server and added the following as a cron job:
0 * * * * scp /var/www/tempr/tg*p.png
I hope this was vaguely interesting/useful. I'll try to add more details and updates over time. If you are building something similar, please let me know in the comments; I'd love to see what everyone else is up to.
Edit: Updated to reflect graphs now appearing on not

Similar Posts

How OEISbot Works
Logic Bot
Logical Contradictions
Logic Bot, pt. 2


Comments in green were written by me. Comments in blue were not written by me.
 Add a Comment 

I will only use your email address to reply to your comment (if a reply is needed).

To prove you are not a spam bot, please type "q" then "u" then "i" then "e" then "t" in the box below (case sensitive):
© Matthew Scroggs 2017