Skip to content

Instantly share code, notes, and snippets.

@johnludwigm
johnludwigm / tuple_riddle.py
Last active February 6, 2018 12:32
Python riddle.
#February 6, 2018
#1. Give the return value of the following expression:
>>> 1, 2, 3, 4 == (1, 2, 3, 4)
##############
##############
##############
##############
##############
@johnludwigm
johnludwigm / HappyNumbers.md
Last active February 13, 2018 01:35
Happy numbers problem description.

Problem:

We'll say that for a positive whole number n, the squareSum of n is the sum of the squares of the digits of n. So if n is 1406, then squareSum(n) is 1^(2) + 4^(2) + 0^(2) + 6^(2) = 1 + 16 + 0 + 36 = 53.

We further say that the k^(th)-squareSum of n is squareSum(squareSum(...(squareSum(n)))), where squareSum is composed k-many times. For example, the 3rd-squareSum of 1406 is squareSum(squareSum(squareSum(1406))) = squareSum(squareSum(53)) (as we know from above) = squareSum(5^(2) + 3^(2)) = squareSum(25 + 9) = squareSum(34) = 3^(2) + 4^(2) = 9 + 16 = 25.

Definition: A number n is happy if for some positive integer m, the m^(th)-squareSum of n is equal to 1.

@johnludwigm
johnludwigm / HappyNumbers.java
Created February 13, 2018 01:34
Solution to the Happy Numbers problem in Java.
import java.util.HashSet;
import java.util.Scanner;
import java.io.*;
public class HappyNumber {
//Note that we did not account for the fact that all permutations of a happy number's digits
//must also form happy numbers since addition commutes. This is mentioned in the README.
private final static long ten = 10;
static long squareSum(long number) {
long total = 0;