Author |
Topic |
jhermiz
3564 Posts |
Posted - 2004-07-15 : 10:27:42
|
Has anyone solved it ?http://www.google.com/googleblog/I had a great time this morning doing this rather thanwriting my application...I think I should applyhttp://www.google.com/labjobs/index.htmlIf the value of e wasnt so long I'd post my solution Heh just kidding if anyone attempts and gets stuck ill provide details Jonwww.web-impulse.com |
|
eyechart
Master Smack Fu Yak Hacker
3575 Posts |
Posted - 2004-07-15 : 16:13:42
|
this would make a good programming challenge here. I would like to see an all SQL solution to this.-ec |
 |
|
jhermiz
3564 Posts |
Posted - 2004-07-15 : 16:29:53
|
I've got a VB and C++ solution so far :)...dont know enough SQL to post a solution though :(.I dont want to give the answer :( .. thought we'd have people trying to solve it mathematically :)..thats where it gets fun!!!!Come on ... I know someone out there wants to take a stab!Jonwww.web-impulse.com |
 |
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-07-15 : 19:36:40
|
Im working on a solution (all SQL)Generate 'e' in SQLGenerate primes in SQLThen Query. not the fastest way... but who cares - Its FunCorey |
 |
|
eyechart
Master Smack Fu Yak Hacker
3575 Posts |
Posted - 2004-07-15 : 19:40:48
|
quote: Originally posted by Seventhnight Im working on a solution (all SQL)Generate 'e' in SQLGenerate primes in SQLThen Query. not the fastest way... but who cares - Its FunCorey
I wrote a function to determine a prime number and I wrote a function that calcuates the factorial (needed to calculate e). However, I am stumped on how to actually calculate e beyond 38 digits. How are you accomplishing this?btw, my formula for calculating e is as follows:e = 1/0! + 1/1! + 1/2! + 1/3! + 1/4! + ... -ec |
 |
|
eyechart
Master Smack Fu Yak Hacker
3575 Posts |
Posted - 2004-07-15 : 19:44:28
|
I figured I might as well post what I have. I would like to see an all SQL solution to this.Code for determining a prime number/* eyechart */CREATE FUNCTION IS_PRIME (@pn bigint)RETURNS varchar(10)as/* revised to accept a BIGINT instead of INT */begindeclare @i bigintdeclare @stop bigintset @i = 2set @stop = ceiling(sqrt(@pn))while @i <= @stop begin if @pn % @i = 0 begin if @i <> @pn return 'not prime' end set @i = @i + 1 endreturn 'prime'end/*select dbo.is_prime(1)select dbo.is_prime(2)select dbo.is_prime(3)select dbo.is_prime(4)select dbo.is_prime(5)select dbo.is_prime(6)select dbo.is_prime(10)select dbo.is_prime(22)*/ Code to calculate factorial:/* eyechart */CREATE FUNCTION dbo.calcfactorial (@base_number decimal(38,0))RETURNS BIGINTASBEGIN/* SPROC code borrowed from Ken Henderson http://www.awprofessional.com/articles/article.asp?p=25288&seqNum=13 */DECLARE @factorial bigintDECLARE @previous_number decimal(38,0)IF ((@base_number>26) and (@@MAX_PRECISION<38)) OR (@base_number>32) RETURN(-101) --number too largeIF (@base_number<0) RETURN(-102) -- Can't calculate negative factorialsIF (@base_number<2) SET @factorial=1 -- Factorial of 0 or 1=1ELSE BEGIN SET @previous_number=@base_number-1 SELECT @factorial = dbo.calcfactorial (@previous_number) -- Recursive call IF (@factorial=-1) RETURN(-103) -- Got an error, return SET @factorial=@factorial*@base_number IF (@@ERROR<>0) RETURN(-104) -- Got an error, returnENDRETURN @factorialEND/*select dbo.calcfactorial(2)select dbo.calcfactorial(3)select dbo.calcfactorial(4)select dbo.calcfactorial(5)select dbo.calcfactorial(6)select dbo.calcfactorial(7)*/ -ec |
 |
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-07-15 : 19:57:21
|
ec - first, in your is_prime function you should increment by 2 starting from 3 and discount all even 10-digit numbers...second, I'm not using factorials. I haven't worked out the exact code for getting past 38 digits, but i know i can do it. let me show you this though: Declare @e decimal(38,37), @lastStep decimal(38,37) Set @e = 1 Set @lastStep = @e Select @e = @e + @lastStep/number, @lastStep = @lastStep/number From admin.dbo.getSequence(1,50,1) Select @e I am planning on taking advantage of powers of 10 at certain points, just don't know where yet. Good-luckCorey |
 |
|
eyechart
Master Smack Fu Yak Hacker
3575 Posts |
Posted - 2004-07-15 : 20:12:54
|
OK, i revised my code with your suggestions. Also, I have a solution if I provide e (I found e to 10000 places at wikipedia).btw, I had to change my is_prime function to accept bigint instead of int. Small oversight ;)EDIT:when I changed my function to increment by 2 starting from 3 it broke. Seems to work fine as is though.-ec |
 |
|
eyechart
Master Smack Fu Yak Hacker
3575 Posts |
Posted - 2004-07-15 : 21:15:35
|
OK, here is my solution:--drop table google_challenge--create table google_challenge (e text)--truncate table google_challengeinsert into google_challenge values( '2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642742746639193200305992181741359662904357290033429526059563073813232862794349076323382988075319525101901157383418793070215408914993488416750924476146066808226480016847741185374234544243710753907774499206955170276183860626133138458300075204493382656029760673711320070932870912744374704723069697720931014169283681902551510865746377211125238978442505695369677078544996996794686445490598793163688923009879312773617821542499922957635148220826989519366803318252886939849646510582093923982948879332036250944311730123819706841614039701983767932068328237646480429531180232878250981945581530175671736133206981125099618188159304169035159888851934580727386673858942287922849989208680582574927961048419844436346324496848756023362482704197862320900216099023530436994184914631409343173814364054625315209618369088870701676839642437814059271456354906130310720851038375051011574770417189861068739696552126715468895703503540212340784981933432106817012100562788023519303322474501585390473041995777709350366041699732972508868769664035557071622684471625607988265178713419512466520103059212366771943252786753985589448969709640975459185695638023637016211204774272283648961342251644507818244235294863637214174023889344124796357437026375529444833799801612549227850925778256209262264832627793338656648162772516401910590049164499828931505660472580277863186415519565324425869829469593080191529872117255634754639644791014590409058629849679128740687050489585867174798546677575732056812884592054133405392200011378630094556068816674001698420558040336379537645203040243225661352783695117788386387443966253224985065499588623428189970773327617178392803494650143455889707194258639877275471096295374152111513683506275260232648472870392076431005958411661205452970302364725492966693811513732275364509888903136020572481765851180630364428123149655070475102544650117272115551948668508003685322818315219600373562527944951582841882947876108526398139559900673764829224437528718462457803619298197139914756448826260390338144182326251509748279877799643730899703888677822713836057729788241256119071766394650706330452795466185509666618566470971134447401607046262156807174818778443714369882185596709591025968620023537185887485696522000503117343920732113908032936344797273559552773490717837934216370120500545132638354400018632399149070547977805669785335804896690629511943247309958765523681285904138324116072260299833053537087613893963917795745401613722361878936526053815584158718692553860616477983402543512843961294603529133259427949043372990857315802909586313826832914771163963370924003168945863606064584592512699465572483918656420975268508230754425459937691704197778008536273094171016343490769642372229435236612557250881477922315197477806056967253801718077636034624592787784658506560507808442115296975218908740196609066518035165017925046195013665854366327125496399085491442000145747608193022120660243300964127048943903971771951806990869986066365832322787093765022601492910115171776359446020232493002804018677239102880978666056511832600436885088171572386698422422010249505518816948032210025154264946398128736776589276881635983124778865201411741109136011649950766290779436460058519419985601626479076153210387275571269925182756879893027617611461625493564959037980458381823233686120162437365698467037858533052758333379399075216606923805336988795651372855938834998947074161815501253970646481719467083481972144888987906765037959036696724949925452790337296361626589760394985767413973594410237443297093554779826296145914429364514286171585873397467918975712119561873857836447584484235555810500256114923915188930994634284139360803830916628188115037152849670597416256282360921680751501777253874025642534708790891372917228286115159156837252416307722544063378759310598267609442032619242853170187817729602354130606721360460003896610936470951414171857770141806064436368154644400533160877831431744408119494229755993140118886833148328027065538330046932901157441475631399972217038046170928945790962716622607407187499753592127560844147378233032703301682371936480021732857349359475643341299430248502357322145978432826414216848787216733670106150942434569844018733128101079451272237378861260581656680537143961278887325273738903928905068653241380627960259303877276977837928684093253658807339884572187460210053114833513238500478271693762180049047955979592905916554705057775143081751126989851884087185640260353055837378324229241856256442550226721559802740126179719280471396006891638286652770097527670697770364392602243728418408832518487704726384403795301669054659374616193238403638931313643271376888410268112198912752230562567562547017250863497653672886059667527408686274079128565769963137897530346606166698042182677245605306607738996242183408598820718646826232150802882863597468396543588566855037731312965879758105012149162076567699506597153447634703208532156036748286083786568030730626576334697742956346437167093971930608769634953288468336130388294310408002968738691170666661468000151211434422560238744743252507693870777751932999421372772112588436087158348356269616619805725266122067975406210620806498829184543953015299820925030054982570433905535701686531205264956148572492573862069174036952135337325316663454665885972866594511364413703313936721185695539521084584072443238355860631068069649248512326326995146035960372972531983684233639046321367101161928217111502828016044880588023820319814930963695967358327420249882456849412738605664913525267060462344505492275811517093149218795927180019409688669868370373022004753143381810927080300172059355305207007060722339994639905713115870996357773590271962850611465148375262095653467132900259943976631145459026858989791158370934193704411551219201171648805669459381311838437656206278463104903462939500294583411648241149697583260118007316994373935069662957124102732391387417549230718624545432220395527352952402459038057445028922468862853365422138157221311632881120521464898051800920247193917105553901139433166815158288436876069611025051710073927623855533862725535388309606716446623709226468096712540618695021431762116681400975952814939072226011126811531083873176173232352636058381731510345957365382235349929358228368510078108846343499835184044517042701893819942434100905753762577675711180900881641833192019626234162881665213747173254777277834887743665188287521566857195063719365653903894493664217640031215278702223664636357555035655769488865495002708539236171055021311474137441061344455441921013361729962856948991933691847294785807291560885103967819594298331864807560836795514966364489655929481878517840387733262470519450504198477420141839477312028158868457072905440575106012852580565947030468363445926525521370080687520095934536073162261187281739280746230946853678231060979215993600199462379934342106878134973469592464697525062469586169091785739765951993929939955675427146549104568607020990126068187049841780791739240719459963230602547079017745275131868099822847308607665368668555164677029113368275631072233467261137054907953658345386371962358563126183871567741187385277229225947433737856955384562468010139057278710165129666367644518724656537304024436841408144887329578473484900030194778880204603246608428753518483649591950828883232065221281041904480472479492913422849519700226013104300624107179715027934332634079959605314460532304885289729176598760166678119379323724538572096075822771784833616135826128962261181294559274627671377944875867536575448614076119311259585126557597345730153336426307679854433857617153334623252705720053039882894990342595662329757824887350292591668258944568946559926584547626945287805165017206747854178879822768065366506419109734345288783386217261562695826544782056729877564263253215942944180399432170000905426507630955884658951717091476074371368933194690909819045012903070995662266203031826493657336984195557769637876249188528656866076005660256054457113372868402055744160308370523122425872234388541231794813885500756893811249353863186352870837998456926199817945233640874295911807474534195514203517261842008455091708456823682008977394558426792142734775608796442792027083121501564063413416171664480698154837644915739001212170415478725919989438253649505147713793991472052195290793961376211072384942906163576045962312535060685376514231153496656837151166042207963944666211632551577290709784731562782775987881364919512574833287937715714590910648416426783099497236744201758622694021594079244805412553604313179926967391575424192966073123937635421392306178767539587114361040894099660894714183406983629936753626215452472984642137528910798843813060955526227208375186298370667872244301957937937860721072542772890717328548743743557819665117166183308811291202452040486822000723440350254482028342541878846536025915064452716577000445210977355858976226554849416217149895323834216001140629507184904277892585527430352213968356790180764060421383073087744601708426882722611771808426643336517800021719034492342642662922614560043373838683355553434530042648184739892156270860956506293404052649432442614456659212912256488935696550091543064261342526684725949143142393988454324863274618428466559853323122104662598901417121034460842716166190012571958707932175696985440133976220967494541854071184464339469901626983516078489245140589409463952678073545797003070511636825194877011897640028276484141605872061841852971891540196882532893091496653457535714273184820163846448324990378860690080727093276731275819665639411489617168329804551397295066876047409154204284299935410258291135022416907694316685742425225090269390348148564513030699251995904363840284292674125734224477655841778861717372654620854982944989467873509295816526320722589923687684570178230380965678831122893058091405726108658848458731016581511675333276748870148291674197015125597825727074064318086014281490241467804723275976842696339357735429301867394397163886117642090040686633988568416810038723892144831760701166845038872123643670433140911557332801829779887365909166596124020217785588548761761619893707943800566633648843650891448055710397652146960276625835990')GO-- select * from google_challenge-- Find and remove decimal from eDECLARE @PTR binary(16)DECLARE @IDX intselect @PTR = textptr(e), @IDX = charindex('.', e)-1from google_challenge where e like '%.%'-- make the changeupdatetext google_challenge.e @PTR @IDX 1 ''go-- Loop through e, trying to find first 10 digit primedeclare @pos bigintdeclare @google varchar(20)set @pos = 1set @google = ''while @google <> 'prime'begin select @google = dbo.is_prime(substring(e, @pos, 10)) from google_challenge set @pos=@pos+1endselect @google, @pos-1, substring(e, @pos-1, 10) from google_challengego Hopefully corey can come up with his method of calculating e to n precision, rather than the cheat that I had to use (thanks wikipedia). -ec |
 |
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-07-15 : 21:33:47
|
just thought it would be twice as fast as it would skip all of the evens... I'll see if I can't get 'e' generated to at least 4000 characters... Corey |
 |
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-07-16 : 00:04:29
|
ok... this is where i'm at: Declare @x decimal(38,37), @y decimal(38,37), @z int, @pos int, @e nvarchar(4000), @shift intSet @e = '2.'Set @x = 0Set @y = 1Set @z = 2Set @pos = 3Select z = @z, y = @y, x = @x, e = @eWhile (@z < 85)Begin Select @shift = power(10,floor(power(2,-((@z+2)%3)))), @x = (@x + @y/@z)*@shift - convert(int,(@x + @y/@z)), @y = @y*@shift/@z, @e = @e + case when @shift=10 then convert(nvarchar,floor(@x)) else '' end, @z = @z + 1 Select z = @z, y = @y, x = @x, e = @e, shift = @shiftEndSelect @e This breaks with z>=85...I will pick it up tommorrow... night!Corey |
 |
|
jhermiz
3564 Posts |
Posted - 2004-07-16 : 08:05:11
|
You guys are doing too much work... first you may want to reread the problem...My blog is sort of down but sort of up...trying to switch servers.You wont need that many characters / digitshttp://www.thecenturoncompany.com/jhermiz/blog/index.htmlfirst see that the first 10 digits after 2. make f(1)...My blog has clues!But for some reason the code I posted doesnt show up on the URL (server down???)Jonwww.web-impulse.com |
 |
|
Arnold Fribble
Yak-finder General
1961 Posts |
Posted - 2004-07-16 : 08:35:21
|
quote: SELECT @factorial = dbo.calcfactorial (@previous_number) -- Recursive call
Why do people do this? There is absolutely nothing to be gained by using recursion to calculate factorials in procedural language. |
 |
|
jhermiz
3564 Posts |
Posted - 2004-07-16 : 08:37:30
|
Why are people calculating the factorial though ?e has already been calculated for us years and years ago.Jonwww.web-impulse.com |
 |
|
Kristen
Test
22859 Posts |
Posted - 2004-07-16 : 08:51:14
|
I'm sure you should be using a TALLY table ... :) |
 |
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-07-16 : 09:10:19
|
Jon - I think you are missing the point...quote: this would make a good programming challenge here. I would like to see an all SQL solution to this.
While I realize that once you have e it is extremely easy to find the first 10-digit prime. What is more difficult is to generate e to a given number of digits in SQL alone. Especially when precision seems to get a bit screwed when you get past 38 digits. That said, I thought I had figured it out late last night, but its still not quite right.current codeDeclare @x decimal(38,27), @y decimal(38,27), @z int, @e nvarchar(4000), @shift int Set @e = '2.' Set @x = 0 Set @y = 1 Set @z = 2 Select z = @z, y = @y, x = @x, e = @e While (@z < 32) Begin Select --@shift = power(10,floor(power(2,-((@z+2)%3)))), @shift = power(10,patindex('%00[1-9]%',right(@y,len(@y)-2))), @e = @e + case when @shift>1 then convert(nvarchar,convert(int,(@x + @y/@z)*@shift)) else '' end, @x = (@x + @y/@z)*@shift - convert(int,(@x + @y/@z)*@shift), @y = @y*@shift/@z, @z = @z + 1 Select z = @z, y = @y, x = @x, e = @e, shift = @shift End Select @e Still doesn't work though... damnCorey |
 |
|
Arnold Fribble
Yak-finder General
1961 Posts |
Posted - 2004-07-16 : 09:29:58
|
quote: Originally posted by Kristen I'm sure you should be using a TALLY table ... :)
What, as part of a spigot algorithm solution? Yeah, that might work. |
 |
|
jhermiz
3564 Posts |
Posted - 2004-07-16 : 09:33:04
|
ehhhjust remember though..you don't need to check numbers that are not divisible by Rad(n)in addition, the most you need to loop is from 2 to Rad(n) for any given n. I don't know enough SQL but I use the C++ mod operator % to return a remainder..if no remainder is returned you've realized that the number is not prime.Jonwww.web-impulse.com |
 |
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-07-16 : 09:58:56
|
whats rad(n)??? I would have thought 2 to ceiling(sqrt(n)) would be the rangeCorey |
 |
|
jhermiz
3564 Posts |
Posted - 2004-07-16 : 10:04:04
|
Rad(n) is just radical.I've always been taught its a "radical" (square root) :)Jonwww.web-impulse.com |
 |
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-07-16 : 10:09:16
|
ahhh... so they are the same. Thats what ec did in his solution with the pregenerated e...now if could only get my generator to work...*sigh*... must actually work though Oh, and I'll have a project for you shortly Corey |
 |
|
Next Page
|