Hehe, programming it in java would have been a lot easier, but I wanted to break the cycle of code solutions...and I haven't done anything with programming (especially strings) since high school
Been doing this in Racket, a Scheme language that we learned this semester, from the suggestion of my teacher. Fun stuff
Never done Racket but Scheme is based on Lisp and my favorite language is Common Lisp. A very powerful and under used language.
How about a simple one that doesn't require any coding... There are 13 coins on the table in a row, 5 are "heads" side up, the rest are "tails" side up. You need to blindly separate them into two groups that have equal number of coins "heads" side up. You can freely move coins around and flip them over as much as you want, "blindly" means that you can't tell which side a given coin is up by sight or by touch.
oh man this thread just made my head explode!......... i hate you all......why oh why do i ever read these threads.....why.......why..............
My answer to #4 is brute force baby: Spoiler 906609 Solved in 2.869s Code: my $answer = 0; for(my $x = 999; $x >= 100; $x--) { for (my $y = 999; $y >= 100; $y--) { my $product = $x * $y; my $length = length($product) - 1; my $middle = int($length/2); my $test = 0; for(my $index = 0; $index <= $middle; $index++) { my $one = substr($product, $index,1); my $two = substr($product, $length - $index,1); if($one != $two) { $test = 0; last; } else { $test = $product; } } $answer = $test if($test > $answer); } } print "$answer\n";
Brute force again. Starting to take longer 11.486s Code: [SPOILER]my $answer = 20; my $test = 1; while($test) { $answer += 20; for(my $x= 19; $x >= 2; $x--) { if($answer % $x != 0) { $test = 1; last; } else { $test = 0; } } } print "$answer\n";[/SPOILER]
I would be interested to see if this ran faster than my code, however I don't know how to calculate run speed lol. You say yours took 2 seconds I'm pretty sure my ran faster than that, however your starting high and going down (which makes more sense). I'm thinking it has something to do with the fact that you used 2 for loops and I altered one.
I did a brute force as well o.0 cept it only took me .0013 seconds Code: [SPOILER]function [ Ans ] = SmallestDivisible() maxval = factorial(20); Ans = maxval; for i = 20:20:maxval if mod(i,20) == 0 && mod(i,19) == 0 && mod(i,18) == 0 && mod(i,17) == 0 && mod(i,16) == 0 && mod(i,15) == 0 && mod(i,14) == 0 && mod(i,13) == 0 && mod(i,12) == 0 && mod(i,11) == 0 Ans = i; break; end end end[/SPOILER] and for my answer i got Spoiler 232792560
Correct! I knew we had some more closet programmers out there. You're just afraid that we are going to put you to work arn't you
That problem is very easily solved with a pencil (and when i edit my program to use the following algorithm, it runs in under 10 microseconds) Code: [SPOILER]function [Ans] = SmallestDivisible2(maxNum) factors = zeros(1,maxNum); Ans = 1; for i=1:maxNum factorization = factor(i); while(size(factorization,2)~=0) Factor = factorization(1); occurances = find(factorization==Factor); if factors(Factor) < size(occurances,2) factors(Factor) = size(occurances,2); end factorization(occurances) = []; end end for j=1:maxNum Ans = Ans*power(j,factors(j)); end end[/SPOILER] The resulting factors' powers matrix is as follows: Code: [SPOILER]factors = 1 4 2 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 1 0[/SPOILER] this will run any sequence of numbers (not just up to twenty) i clocked its runtime on the numbers from 1 to 20,000 and it ran in only .0011 seconds if you're curious the answer is: Code: [SPOILER]487932562728827051853192251818304516827404325417772845240943042996188 805741117114045256944178325252416732608321475648115070946795791719917367 168886003228136458216545095330834551704837213339636337731408608409714535 797995575820000161265893188617880666804973935334838119599722456535086799 529562371999322815908049092412925543395108676851780329258683841138937349 446199780208474193932291708645043401961888001892179929187830935027865949 709520484960213483365280098825308106450677818733710304822586150436823289 923722073169256990730930126006893812694626060699755967640951022613854375 241456913382118606719324408552106758332368926174696001035629306530643991 980581671571842550774416422028876104184338202595099772021127475895597315 189727087267646647586802027990339378250530628298075734338494243479421000 882027063174850772351212528177459065243865757181453965746061270186387862 102455885739305032125766685791778364109143006565602314810372748244991767 626273866589611088927635088589773509329909625721273183251530231780853535 645343781731265886177654469040204180399751121641424223456373567737960054 209186449747079271756001176586473885205283870901354238090036056604750488 718367356628124826435206886750494186036827267461403248518475337773918015 682068403036360530539149572556287574377463600214697165694342458958640783 331497021640749662242611599502612994954355215575046749623100133713690448 361378900344793005054863222597354226864378827113221437620352986752672725 575475929838604277901243662953990708358167557731854335339130283001618634 624195177829551677784178038360237710380921949038837717072352571458459432 107615091313569353212735647371248929270518654167579158937559398914943014 601650275920747990660481235848832340888603244813224177069455410543803005 815148517330571133753551928920523691436944909659691641681119341020633136 704706043871583547909725967881052543972397920039557096042129546118312545 368629713385953842807410561994308065830832144467466345288104684741636225 392502625927257890405482719946626771635594866796547977939105237765975985 319454115138443325562918427583040129667880536977983547263138532080339696 512849149691032370789129559057191337948422965103830690568376890981269700 367694387878516685384908215306734771862794867529886078847092136905765077 987440927486784015243447482811945013413334317319738008723344054078816885 676614815023545676810780225747591462130638334193428396579776439925487028 693280721341397734182356272699093588655430663520209934931751274755006780 591443773438705864585140014285647557164675212939626089003299831711690762 577442842010503345814055022212504497824655783092039233352570579984368576 527135568840833688083251492803512185694610979548582261496456603851167480 695355203107879686444255960939160431775531330007035715781214807625558106 261087736567075710001410512634573337271313910729075567068808472430175369 918556967626627183121662418665584643705077163356127720294434444589466780 506702040775779834349878859235973951495342212452325135639832796706033960 911725749488292161475744749992884806976741904536442688922202111941534621 876073608100787461731351721284563089256773410285289645847579072292909182 544750248306216600501466071516687087415510643884080847044931382786531887 440635560043064044751754278984263655939959110086462533360738341150016577 215471328037324940575248906084258746678542160559523463605306187320591271 393396067762626123353428332980472024487799898509755251540760169651817256 310971122305954772665065327002745826765447607449331976261075661724914305 329626993053261581233968175388882956050448143115770473163690408966721356 716709374177128199796558197079124836172645957471669457970007611158323024 219440881955144762833684318449026585205601409171403335918279609730424586 952911967676925445385504525856485221142678894391176290260695548454717180 106000542585701921335838754245628236960709161168148690989388000970425657 091211299165158996017751229502128756906117767275552038130833200321617185 880270812964962289090887811702643482067618931196746327390131603864130917 600953803226122373239232587144960896806291752173941316651214176315460122 584975831361310418175649382651292829906928857425442954432078542049577687 724893726667152960155279584591395570520617182078937500035435927105064855 267279216535934468535062245800487480202774686671071603448541642675233280 733919002370846334444022373987831797496744642614605667709841701393715403 698931315710047559680470576775660153551531715870843227116950969531574766 427279330519293901318202735971147089690773079667720966787775064352133890 339495353182790648475024212788978122638927470316874796076667466619097521 339802080872026194851900134546515181021386627021931343649531967043976409 650937205558788048817237883642405071022219563918092416335108808180261928 133081649108707532174844906807884712007478415239145170509810928530909057 846268839858264686879480687955743009673260749663993429706771888260337484 515588558708702070888975050709620764996879250972004179374055962510052654 043090407036922708146281861360408776125213786140492016107062509614857832 228850641917126711659114916397925051683033402437975565210694631119209271 993922364924701921063517106592129051092242218067702204819965884694063156 122286531463184179072895183641071150351388422025477965505857590659951258 099560888390973578674220662119873870697874166015661562265891217879617656 633533991537590347696504397890652940568335649753795688739567840216739367 259266319495503298292983359008694373554716023850944669463307516978837555 253173306957856525997564374977144163336328120063584709430470203456564311 556869820529466965093023569932899672474867427861560548385689854259023766 147484528237393124899974541516546156098986066203055511969608445881736427 185359796447206493437296686227677088457143775561268962113651303757776359 075584876223040550540828476483538302382148189436328012763226094827453794 345419288633476090202557650487028919239267784510234183973912449967109186 387167135545768138811977468909446060927187134685675707851096980797167093 155112247684506375405913896220484631423506888211378545586114766083194728 096099501261504585967758882483545105317156620064046169270081535257094086 157344732394869607402090532739329598307293519966708677333694920941985463 154724880329321177214093655744163216340633115053578975104271899879630026 232219235807411667876128547441086647520367320314367258236102366119843592 895192395551374974680067273120539009525191638865040372593183500197863566 028303916075495287475553796202747024865206190282898225440064180537373528 950287439764557503762794924427583125025385570190672942247009400083107891 546879893853522646280211899954836884103023348536414712923030768212628079 314501247521346219041940878581248759075867011417433728965638841990954498 646491720886857116256114797672287446796679629991507894492278867716669615 185669990097241580934008812187303200306147917646523230326161077173605297 169900426372425725418570541315739852902272784008672358335805019697208963 212497959894515544414346782001252082906425250526664058807151765476383421 885859780208338390226184407439124801370862753958218909814132503774890485 293909634221979090683840342330056454876205448470449260922359079049601906 313655394294813521190098639835554223259991526876667260302390917558645645 902269360309006895522183372760989473145702723158159475403408726641912496 350635872498608929438627636892840415866852417836799161960153943821123157 025893640666360971321526988292187147784162803315136029921544368122479283 900075664468275524768362705582284628330866374682681119683303238760539530 622153890800992472000503994362522452875379536205652917648297577925364407 858068285341096151744512342095709014111353814905820302388419411371902623 027242556075349121264076973240862495961260519917679133544662008703661620 698746046442845474829977440178626093099363398000307535293441348028787952 446276486208119920482809959459796068287114315477157325733678791138549005 955400352352960857521337480412001681802795335049671789742246985910078125 807858149003608337068414904474169434317632005612823101244151610409702563 565595689345895954833368986727050473421480209290914243567119541756206133 715823236373639852060372025614887107314982092449608643996926470774915616 569345415574672192585407824318090748303530910949877928804549454690766439 854998556464937074548588953442820938601195734840372230197958645690937997 576168268600689857810586015988064651585709377579051660306772379911513309 177129676220383744569324305186371707262272975650610727571142899768329451 385959429217235045272823427827237858466025144969804892842707891091421817 156834713612696300241601933337306925504344712840561253520246263651692038 313754745876048820905635366344382787271061659042672234350510071348526694 663032526748251674280541980256991957150501085468630428745253925144781656 358991914075916269547411295098112000000[/SPOILER] the solution can be found using the prime factorization of all of the numbers (granted finding the prime factorization of all 20 numbers isnt quick, its a lot quicker than brute forcing it) and taking the maximum number of occurances of any one factor in any of the numbers the solution will be: Spoiler (1^1)*(2^4)*(3^2)*(5^1)*(7^1)*(11^1)*(13^1)*(17^1)*(19^1)=232792560 where 2^4 is found in 16, 3^2 is found in 9 and all of the powers of 1 are the prime elements of 1:20
That blew my mind lol. I never messed with algorithms although I expect I'll take class' about it eventually. You put a lot of work into that when you already had the answer. I personally wouldn't have went that in depth with it unless required to. I would assume that it would take me far longer to do that than it did you since I would have spent like 80% of the time googleing math equations.
for a lot of these... i havent really been needing to write programs... like for 6 i just did Code: sum(1:100)^2-sum((1:100).^2) in matlab and got Spoiler 25164150