The problem can be found here and is essentially asking us to generate J many numbers containing N many digits, where these digits have to be either 0 or 1. The catch is that the these numbers that we generate have to be composite when interpreted as being written in base i, where i ranges from 2 to 10, inclusive. Furthermore, these digits have to begin and end with a 1. My solution was to randomly generate candidate numbers and test for compositeness across the bases. However, I am wondering if perhaps there is a better way to solve the problem (with the interest of speed)? For perusal, my code is as follows (note that I believe it to be correct, but slowy):import sysimport random# converts a list of 1's and 0's interpreted in base i, to base 10 equivalentdef base_i_to_base_10(binary, i):    return sum([binary[j]*(i**(len(binary)-j-1)) for j in range(len(binary))])# checks primality of a number# if prime, return -1# if composite, return the divisor that indicated sodef isPrime(num):    for i in range(2,int(num**0.5)+1):        if num%i==0:            return i    return -1# Function to check if a number (whose digits are 0 and 1) is prime# Primality is tested by converting the number into bases 2 through 10# If prime, we return []; representing that no divisors exist# If composite, we return a ['some divisor'] as proofdef notPrime(binary):    theFactors = []    # iterates over bases 2 through 10    for i in range(2,11):        num = base_i_to_base_10(binary, i) # number after converting to base i        divisor = isPrime(num)        if divisor==-1:            return [] # the number was prime in base i        else:            theFactors += [divisor] # the number is not prime in base i    return theFactors # the number is composite in bases 2 through 10# Reads in how many test cases there arecaseNums = int(sys.stdin.readline().strip(\n))case = 0 # initalize case count to zero# read lines from standard inputfor line in sys.stdin:    case += 1    # each line contains two numbers    # N is the number of digits the binary number contains    # by binary we mean made up of 1's and 0's, and not necessarily in base 2    # J is the number of binary digits that are not prime in any of the bases    # 2 through 10 and begin and end with 1, that we would like to generate    N, J = [int(i) for i in line.strip('\n').split(' ')]    print Case #{}:\n.format(case) # print out test case number    valid = [] # list of valid binary numbers satisfying the requiremnts    seen = [] # list of binary numbers we've considered already    # randomly generate a candidate binary number    binary = [1]+[int(random.random() > 0.5) for i in range(N-2)]+[1]    # A list of divisors, one from each base 2 through 10    # If the number is not prime in atleast one of the above bases, divs = []    divs = notPrime(binary)     # while we haven't generated J many valid binary numbers, keep searching    while (len(valid)<J):        # if the randomly generated binary number isn't one we've seen already        # and if it is prime across all bases 2 through 10        # record the binary number as valid        if ( not (binary in seen) and len(divs)==9):            # dispay binary number and some of its divisors            num = ''.join([str(i) for i in binary]) +              for i in divs:                num += str(i)+             num = num.strip(' ')            print num            valid += [binary] # mark binary number as valid        seen += [binary] # mark the binary number as already seen        # generate a new binary number to consider, and some of its divisors        binary = [1]+[int(random.random() > 0.5) for i in range(N-2)]+[1]        factors = notPrime(binary)
On Finding Coin Jams
python;algorithm;programming challenge;random