{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def get_first_compose(n):\n",
    "    primfac = []\n",
    "    d = 2\n",
    "    while d*d <= n:\n",
    "        while (n % d) == 0:\n",
    "            primfac.append(d) \n",
    "            n /= d\n",
    "            return d\n",
    "        d += 1\n",
    "    return 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def produce_numbers(vec, base):\n",
    "    return np.sum([_v* base**_power   for _power, _v in enumerate(vec[::-1])  ])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "vec = [1,0,0,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def check_vec(vec):\n",
    "    valid = 1\n",
    "    res = []\n",
    "    for base in np.arange(2,11,1):\n",
    "        first_comp = get_first_compose( produce_numbers(vec,base))\n",
    "        if first_comp:\n",
    "            res.append(first_comp)\n",
    "        else: \n",
    "            return 0\n",
    "    return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "N = 16 \n",
    "J = 50"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def list_int_to_str(list_int):\n",
    "    return [str(_i) for _i in list_int]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "10\n",
      "11\n",
      "12\n",
      "13\n",
      "14\n",
      "15\n",
      "16\n",
      "17\n",
      "18\n",
      "19\n",
      "20\n",
      "21\n",
      "22\n",
      "23\n",
      "24\n",
      "25\n",
      "26\n",
      "27\n",
      "28\n",
      "29\n",
      "30\n",
      "31\n",
      "32\n",
      "33\n",
      "34\n",
      "35\n",
      "36\n",
      "37\n",
      "38\n",
      "39\n",
      "40\n",
      "41\n",
      "42\n",
      "43\n",
      "44\n",
      "45\n",
      "46\n",
      "47\n",
      "48\n",
      "49\n",
      "50\n",
      "CPU times: user 49.4 s, sys: 159 ms, total: 49.6 s\n",
      "Wall time: 51.6 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "import itertools\n",
    "ct_res = 0\n",
    "\n",
    "with open('/Users/mac/Downloads/res_jamcoins_small.txt','w') as g:\n",
    "    g.write('Case #1:\\n')\n",
    "    for vec in itertools.product(*[[1,0]]*(N-2)  ) :\n",
    "        vec = [1]+ list(vec)+[1]\n",
    "        primes = check_vec(vec)\n",
    "        if primes:\n",
    "            ct_res +=1\n",
    "            print ct_res\n",
    "            g.write( ''.join(list_int_to_str(vec))+ ' '  +' '.join(list_int_to_str(primes))+' \\n')\n",
    "        if ct_res == J:\n",
    "            break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Case #1:\r\n",
      "1111111111111111 3 2 5 2 7 2 3 2 11 \r\n",
      "1111111111111001 3 2 5 2 7 2 3 2 7 \r\n",
      "1111111111110111 7 13 3 31 5 3 59 7 3 \r\n",
      "1111111111110101 5 2 17 2 19 2 5 2 101 \r\n",
      "1111111111110011 3 2 5 2 7 2 3 2 11 \r\n",
      "1111111111101101 3 2 5 2 7 2 3 2 11 \r\n",
      "1111111111101011 5 2 17 2 37 2 5 2 101 \r\n",
      "1111111111100111 3 2 5 2 7 2 3 2 11 \r\n",
      "1111111111100001 3 2 3 2 7 2 3 2 3 \r\n"
     ]
    }
   ],
   "source": [
    "!head /Users/mac/Downloads/res_jamcoins_small.txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
