A little brain exercise
November 20th, 2007
”Practice doesn’t make perfect. Perfect practice makes perfect.” – Vince Lombardi
Here’s a cool logic problem that a friend shared recently:
There are 14 cards on a table. Each card can be numbered with a number 1 through 10. Each three consecutive cards must sum to 18. The fourth card is a 7, and the twelfth card is an 8. What is the eighth card?
I had problems reasoning it out, so I decided Ruby would save the day! So I wrote some code to try a brute force search, and here’s what I came up with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#!/usr/bin/ruby O = [nil, nil, nil, 7, nil, nil, nil, nil, nil, nil, nil, 8, nil, nil]; def checker(arr) (0..(arr.length-3)).each do |i| return false if (arr[i].nil? or arr[i+1].nil? or arr[i+2].nil?) if ((arr[i] + arr[i+1] + arr[i+2]) != 18) then return false end end return true end k = O.clone (0..(k.length-1)).each do |i| k[i] = 1 if k[i].nil? end stop = false MAX = 10 p = 0 cnt = 1 while (!checker(k)) do cnt += 1 while k[p] == MAX do k[p] = 1 p += 1 while (O[p] != nil) do p += 1 end end k[p] += 1 p = 0 end puts k.join(" ") |
Feeling quite satisfied with myself, I set my machine into motion. Then I added some puts trace statements to see how quickly I could expect my answer. Finally, the reality of the situation came into focus, as rough estimates showed me that it would take about 400 hours to arrive at the answer with this code running on my setup.
Sure it could be tweaked to use heuristics of the problem to search more efficiently, but the point of my idea was to try and be lazy and let Ruby do all the work. I even tried a random search before the current version of the code, that wasn’t happening either. Although imagine how crazy it would have been if it did! Sometimes it’s easy to underestimate a problem.
Anyway, after revisiting the problem and just applying some simple logic, the answer came pretty quickly. Sometimes it’s also easy to overestimate a problem.

May 5th, 2008 at 07:24 AM scuk; renters insurance nmjof; online insurance quotes, 8-DD; phentermine %O; auto insurance mpu; online alprazolam gtqd; best online casino xbjn; health insurance 7313; free casino games 3784; play slots >:PP; online black jack %); free annual credit report bvzk; roulette 8[; casino ngwqnb; poker 171955; auto insurance quotes 461; homeowners insurance =-DDD; loans wfiejl; free credit check 62526; cheap levitra >:-); secured credit cards bfcl; casino >:-]; homeowners insurance 8PPP; online poker dnsxgp; online black jack 2170; term life insurance 8-[[; order viagra, 326; texas holdem 2396; car insurance quotes mzg; insurance quotes skreh; home owners insurance 0541; home owners insurance snqha; affordable health insurance oitx; car insurance quotes 570; free casino games 8]; free slots =(((; credit check 8]]; credit report nlzd; free online auto insurance quotes, %-DDD; free online auto insurance quotes %-[; free online slots rea; individual health insurance 925772; health insurance quotes, %-[[[; no deposit casinos 983; buy viagra =-[[[; insurance rates =-[[[; gambling 8-(((; cheap auto insurance 5473; online black jack =)); free online auto insurance quotes 8-(((; term life insurance fthg; texas holdem 437; online life insurance shsdu; slots 523; car insurance zed; lorazepam, >:-((; purchase cipro >:]; progressive insurance 762; home insurance oux; car insurance quotes 40793; free casino games >:(((; consolidate debt 8((; renters insurance bxbjc; buy ultram 9805; online casino pqthya; auto insurance quotes vujag; free annual credit report 1252; cheap auto insurance 6605; annual free credit report 8]];
May 5th, 2008 at 08:11 AM a , buy propecia, tramadol, viagra alternative, buy viagra, buying augmentin, buy soma cheap, viagra without prescription, prednisone online, generic soma, viagra online, cheap meclizine, cheap cialis, rimonabant, buy prozac online, purchase prozac, acomplia, purchase tramadol online, purchase ultram, cheap soma, cheap soma, viagra, order viagra, purchase cheap tramadol,, cialis side effects, order tramadol online,, hoodia, buy augmentin now, viagra, zyban, zoloft online, cheap plavix, cheap viagra, buy generic viagra, viagra on line, cheap prednisone, cheap generic cialis, generic viagra, order ultram online,, cipro, buy cialis, buy augmentin online, purchase viagra,, order prozac, acomplia weight loss drug, soma, viagra online, buy nexium online, levitra online, purchase prednisone, cheap buspar, buy tramadol online, cheap cialis, buying soma, buy cialis online, cheap soma, buy acomplia, cialis levitra viagra, buy online viagra, buy viagra now, cheap ultram, viagra cialis levitra, prozac pills, buy soma cheap,, buy propecia online,, viagra pills, order levitra online,, ultram, buy cialis,, buy cialis online, cheap ultram online, order levitra, tramadol, purchase soma, buy zoloft, buy rimonabant online,, ultram online,
May 6th, 2008 at 07:12 AM a , buy acomplia, generic cialis, generic levitra, buy cialis, ultram, propecia, cheap soma, cheapest cialis, buy ultram online, purchase soma,, levitra, prozac, online cialis, zoloft, buy cheap rimonabant online, buy viagra online, rimonabant, order tramadol online,, buy prozac, cheap soma, cheap viagra, order levitra online, ultram, generic prozac, purchase tramadol, cheapest cialis, acomplia, purchase augmentin, rimonabant, rimonabant, order tramadol,, viagra and levitra, viagra and levitra,, buy soma online, viagra without a prescription,
May 6th, 2008 at 06:48 PM a , soma, propecia, viagra without a prescription, buy viagra, buying augmentin, buy acomplia online, prozac, buy soma, order cialis,, prednisone, buy ultram,, buy levitra, tramadol online,, buy acomplia, buy soma, generic levitra, buy soma, cheap tramadol, buy rimonabant online, tramadol online, purchase tramadol online, viagra online, buy acomplia, buy rimonabant online, buy acomplia online, purchase viagra, cheap ultram, prozac, levitra, rimonabant, generic zoloft, viagra cialis levitra,, cheap generic cialis, cialis online, order tramadol online,, buy rimonabant online, cialis online, buy ultram online, generic cialis, buy prozac,
May 7th, 2008 at 05:27 AM a , diflucan, viagra,, buy prozac online, viagra online, purchase prozac, ultram, buy zoloft, purchase soma, buy generic viagra, generic propecia, purchase prozac, buy ultram, buy soma cheap, buy soma, buy rimonabant, levitra, online lamisil, buy online viagra, buy zoloft, zoloft, buy cialis online, buy soma online, order cialis, viagra tablets, cheap tramadol, prednisone, buy prozac online, augmentin, cheap augmentin, augmentin online, buy ultram online, purchase cheap tramadol,, buy cialis, buy rimonabant,, viagra without a prescription, viagra sale, purchase levitra online, buy tramadol, buy cheap rimonabant online, purchase cheap levitra, rimonabant, buy zoloft, buy viagra online, ultram 50mg, order viagra, buy ultram, cheap tramadol, buy ultram on line, generic zoloft,, buy prozac online, online viagra, augmentin, generic propecia,, levitra online, tramadol, buy prozac, buy tramadol, generic viagra, cheap levitra online, buy propecia, buy viagra, order ultram, online cialis, order tramadol online,, viagra cheap,, levitra online, buy cialis, prozac, cheap tramadol, paxil, tramadol, buy soma online,, purchase levitra, cheap soma, prozac, purchase cheap tramadol,, buy generic viagra, buy cialis, where to buy viagra, prednisone pills, buy viagra online, cialis online,, cialis online, generic viagra, generic prozac, tramadol, buy prednisone online,, buy tramadol online, cheap soma,
May 7th, 2008 at 10:49 AM scuk; acomplia 9269; insurance California zczc; annual credit report, >:-); order alprazolam %[[; cheap neurontin ktklp; ltc insurance %-[; order flonase 412776; generic viagra uyp; health insurance quotes %))); life insurance 8605; nj residents free annual credit report 435; discover credit card company 19448; free credit checks 0859; generic levitra >:PP; cheap auto insurance pmjlvw; cheapest auto insurance 89078; secure credit card fanrhg; sibutramine qepm; insurance texas tjzz; online cipro 4185; buy elavil yxbfh; cheap auto insurance online quotes 2850; free credit check report 8[[[; my free credit report >:-); buy altace :OO; loans >:]];
May 7th, 2008 at 04:37 PM a , viagra on line,, cialis side effects, buy online viagra, buy prozac now, where to buy soma, buy prozac online, cialis generic, viagra and levitra, buy cialis, order prozac, buy cialis online, buy viagra online, cheap meclizine, cialis side effects, cheap cialis, order tramadol, online zovirax, ultram, buy soma, celebrex, buy prozac, buy propecia, buy ultram, acomplia, levitra, cheap prozac, buy viagra, generic viagra, augmentin, viagra online, buy levitra, buy cheap rimonabant online,, propecia online,, generic levitra, buy tramadol online, buy viagra, zyrtec, buy paxil online, ultram 50mg, purchase viagra, order viagra, buy ultram, viagra without prescription, prozac, levitra, viagra cheap,, generic viagra, viagra,, cheap cialis, tramadol hcl, buy rimonabant online, cheap cipro, buy prozac online, zoloft, cheap cialis, generic viagra, generic propecia,, tramadol, purchase tramadol online,, buy levitra, buy augmentin online,, buy tramadol, levitra, purchase levitra online, purchase ultram online, buy viagra cheap, cheapest cialis, ultram, order ultram, buy prozac online, prednisone, buy zoloft online, buy viagra online, buy ultram on line, ultram 50mg, buy pfizer viagra, viagra cheap,, generic zoloft, cheap tramadol online , online viagra, purchase cheap levitra, cialis, generic cialis, purchase prednisone,
May 7th, 2008 at 07:00 PM scuk; buy acomplia wsgml; alprazolam btqd; annual free credit report =-]]; neurontin, etmbp; health insurance mezuao; generic viagra 687; flonase 25364; online skelaxin lvx; California insurance 8-OO; get free annual credit report 2698; discover credit card payment 8-(((; free credit check 84354; pay day loans ktbip; generic levitra zdhk; cheapest auto insurance nwgasd; buy cipro %)); elavil pdkt; cheap ultram %D; my free credit report hjfj; altace, enb; pay day loans =-OOO; life insurance ptxdy;
May 8th, 2008 at 06:50 AM a , cialis, buy tramadol, buy acomplia, buy ultram online, cheap ultram, buy augmentin online, generic cialis, generic prozac, cheap generic viagra,, purchase levitra online, levitra, buy cheap rimonabant, buy rimonabant online, order tramadol online, online cialis, generic levitra, generic viagra, acomplia, buy cialis, buy soma, viagra online, order prozac, cheap soma, buy soma, rimonabant, ultram, soma pills, buy viagra online, cheap prednisone, buy cialis, tramadol, buy acomplia, online tramadol, rimonabant online no prescription, order tramadol online,, generic zoloft, prozac, propecia, acomplia,
May 9th, 2008 at 07:03 PM a , order levitra, tramadol, cialis generic, cheapest cialis, prednisone online, prozac, where to buy viagra, zoloft online, augmentin, buy cialis, rimonabant diet pill, generic propecia, prozac, cheap cialis, buy viagra online, zoloft, levitra pills, buy tramadol online, order cialis, buy prozac now, tramadol online, acomplia weight loss drug, buy prednisone now, buy levitra, cialis, viagra cialis levitra,, cialis side effects, purchase soma,, purchase prednisone, buying augmentin, purchase cheap ultram, generic cialis, purchase tramadol online, buy prozac online, generic cialis online, cheap augmentin, purchase viagra, viagra,, buy cheap acomplia online,, cheap prednisone, buy pfizer viagra, generic soma,, buy rimonabant online,, buy ultram on line,, buy cheap acomplia,, order ultram,, online tramadol, generic viagra, cialis levitra viagra, online cialis, buy generic viagra, buy augmentin now, where to buy viagra, soma pills,, buy augmentin online, order augmentin, propecia online, alternatives to viagra, buy cheap acomplia online, order tramadol online, cheap ultram online , cheap soma, buy tramadol, buy viagra, prednisone pills, viagra cialis levitra,, generic prozac, cheap ultram, cheap tramadol, cialis levitra viagra, buy pfizer viagra, prednisone, cheap prozac, cheapest cialis, buying viagra, generic prozac, cheap soma, generic soma,
May 10th, 2008 at 12:39 PM scuk; alprazolam %-]]]; annual credit report zirdcu; buy neurontin :-[[[; freecreditreport 120646; bankers long term care insurance twg; flonase, 216; generic viagra %]]; california insurance quotes, 8)); affordable health insurance =))); life insurance 350278; discover credit card sign on rpwd; pay day loan jkfspj; free credit check awvr; really cheap auto insurance 8]; buy levitra online, qzlpqk; cheapest auto insurance online, 2086; secured credit cards, 339; sibutramine online qzt; short term health insurance texas 8[[[; buy cipro online ddtk; buy elavil kipuxo; buy ultram vtrb; online life insurance, 874726; pay day loan =-OOO;
May 10th, 2008 at 03:06 PM a , cheap cialis,, zoloft, online cialis, purchase cheap levitra, buy levitra,, order levitra online, buy elavil online, augmentin, buy ultram on line, generic soma,, buy ultram on line, buy propecia online, generic cialis, buy acomplia online, order hoodia,, buy augmentin now, purchase tramadol online, cheapest viagra, generic cialis online, famvir, buy celebrex online, purchase levitra online, purchase tramadol,, ultram, acomplia, altace, order levitra, tramadol, tramadol online, prozac pills, buy levitra, alternatives to viagra,, cialis on line, buy soma online, cheapest cialis, buying viagra,, buy prozac, generic zoloft, purchase cheap tramadol, buy tramadol, viagra pills, prednisone, purchase soma, viagra and levitra, viagra and levitra, propecia online, purchase tramadol, buy online viagra, cheap tramadol, buy prozac, generic propecia,, buy cheap acomplia online, buy online viagra, buy prozac now, ultram 50mg, buy soma online, purchase cheap ultram, generic propecia, purchase tramadol online, cheap ultram, online viagra, order tramadol online,, buy soma cheap, purchase levitra online,, cheap tramadol, cheapest viagra, generic cialis online, rimonabant, buy viagra cheap, tramadol, buy viagra, augmentin, buy prednisone,, cheap levitra, cialis on line, buy viagra in usa, order viagra online, rimonabant online no prescription, alternatives to viagra, viagra online, soma, purchase prozac, ordering viagra, cheap cialis, cheap levitra online, generic cialis online, rimonabant online no prescription, soma, buy buspar online, buy viagra now, prozac, cheap generic cialis, augmentin, cheap tramadol online, tramadol online,, where to buy soma, buy propecia, online tramadol, levitra online, online cialis, augmentin online,, cheap prednisone, buy online viagra,
May 11th, 2008 at 11:45 PM a , zoloft, purchase viagra, cheap cialis, purchase ultram online, cheap soma, viagra alternative, cheap generic cialis, buy viagra now, buy ultram on line,, discount cialis, online cialis, generic soma, buy viagra in usa, propecia online, generic cialis, rimonabant, generic viagra, ultram, order viagra online, buy soma online, buy rimonabant online, viagra online, viagra pills, prednisone,, cheap cialis, viagra without a prescription, buy zoloft online, online cialis, buy zoloft, order prednisone, rimonabant diet pill, order levitra online, order viagra, viagra and levitra,, prednisone online, buy viagra online, order augmentin, buy rimonabant, cheap viagra, generic prozac, buy viagra in usa, viagra and levitra, buy ultram on line, cheap cialis, levitra, cialis on line, rimonabant diet pill,, cheap tramadol, buy cheap acomplia, generic cialis online, cheap levitra, viagra tablets, order levitra, purchase levitra, buy soma, soma, buying soma, generic prozac,, buy viagra, buy cheap rimonabant online, cheap ultram online , where to buy soma, viagra without a prescription, augmentin, generic levitra, purchase prednisone, cialis side effects, buy prozac now,, cialis levitra viagra, purchase prozac, prozac pills, buy levitra, prednisone pills, order cialis,, buy pfizer viagra,, buy cialis online,, rimonabant, cheap cialis, buy viagra, buy ultram,, purchase prozac, levitra online,, buy acomplia, prednisone,
May 11th, 2008 at 11:45 PM a , zoloft, purchase viagra, cheap cialis, purchase ultram online, cheap soma, viagra alternative, cheap generic cialis, buy viagra now, buy ultram on line,, discount cialis, online cialis, generic soma, buy viagra in usa, propecia online, generic cialis, rimonabant, generic viagra, ultram, order viagra online, buy soma online, buy rimonabant online, viagra online, viagra pills, prednisone,, cheap cialis, viagra without a prescription, buy zoloft online, online cialis, buy zoloft, order prednisone, rimonabant diet pill, order levitra online, order viagra, viagra and levitra,, prednisone online, buy viagra online, order augmentin, buy rimonabant, cheap viagra, generic prozac, buy viagra in usa, viagra and levitra, buy ultram on line, cheap cialis, levitra, cialis on line, rimonabant diet pill,, cheap tramadol, buy cheap acomplia, generic cialis online, cheap levitra, viagra tablets, order levitra, purchase levitra, buy soma, soma, buying soma, generic prozac,, buy viagra, buy cheap rimonabant online, cheap ultram online , where to buy soma, viagra without a prescription, augmentin, generic levitra, purchase prednisone, cialis side effects, buy prozac now,, cialis levitra viagra, purchase prozac, prozac pills, buy levitra, prednisone pills, order cialis,, buy pfizer viagra,, buy cialis online,, rimonabant, cheap cialis, buy viagra, buy ultram,, purchase prozac, levitra online,, buy acomplia, prednisone,
May 12th, 2008 at 02:23 PM scuk; insurance California nzdpwk; health insurance texas bxmwa; online casino 497907; free health insurance quotes bbieu; gambling online bijdrq; buy viagra cheap ivid; casino :PP; buy zocor %-DD; ultram online ibggdu; free credit check 072; annual free credit report >:[[[; annual credit report 904941; history of discover credit card :))); play roulette online bouf; loans chvm; whole life insurance quotes %-PPP; zanaflex online vnii; buy levitra online ajwyq; cheap auto insurance 33761; secured credit cards >:-(; buy celexa :OOO; best online casinos, sie;
May 14th, 2008 at 11:33 AM X7pG2K hlxzzvehjfoo, [url=http://slabkgkisnsz.com/]slabkgkisnsz[/url], [link=http://uzuvcjhxqwzy.com/]uzuvcjhxqwzy[/link], http://kpewdszwxmih.com/
May 14th, 2008 at 04:41 PM a , purchase tramadol,, levitra pills, buy prozac, buy viagra cheap, cialis online, viagra no prescription, order viagra, viagra without a prescription, purchase cheap ultram, viagra no prescription, buy augmentin online, buy generic viagra, buy rimonabant online, buy soma cheap, buy viagra in usa, purchase cheap tramadol,, buying prednisone, cheap ultram online , propecia online, ordering viagra, cheap levitra online , buy prozac online, cialis online, buy augmentin online,, buy prozac online, cheap cialis,, order viagra, rimonabant online no prescription, cheap generic cialis, buy viagra, purchase augmentin, cheap prednisone, buy viagra cheap, buy acomplia online, online tramadol, buy prednisone now, where to buy viagra, generic prozac, online viagra,, order cialis, order prednisone, cheap cialis, buy viagra, buy online viagra, viagra alternative, purchase prozac, generic cialis online, purchase soma, buy viagra cheap, ordering viagra, rimonabant online no prescription, prednisone, cialis on line, online viagra,, purchase ultram, buy cheap rimonabant, cheap generic cialis, cheap viagra, ordering viagra, cheap ultram, prozac pills, buy viagra in usa, buying viagra, buy augmentin, viagra sale, order tramadol, generic viagra,, buy pfizer viagra, buy ultram on line,, cheap ultram online , augmentin, buy cheap rimonabant, viagra for sale, purchase augmentin, buy prednisone now, zoloft online, cheap ultram online , zoloft online, rimonabant diet pill, purchase prednisone, buy prozac online, ordering viagra, generic levitra, buy rimonabant, prednisone pills, levitra pills, cheap levitra,, buy generic viagra, online cialis, buy prozac, cheap levitra online , buying viagra online, buy tramadol online, buying viagra, cheap augmentin, buying viagra online, prozac, buying viagra online, where to buy viagra, buying soma, purchase ultram online, discount cialis, buy prednisone now, generic zoloft,, buy cheap rimonabant online, rimonabant diet pill, order tramadol online, generic levitra,, generic levitra, viagra for sale, purchase tramadol online, viagra for sale, zoloft online, acomplia weight loss drug, purchase soma, generic levitra,