Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Site Related Forums
 The Yak Corral
 Corey's Math Problem

Author  Topic 

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-07-16 : 11:14:08
Solve anyway you would like (I would enjoy seeing a SQL version though):

On the topic of primes -

p(1) = 1
p(2) = 2
p(3) = 3
p(4) = 5
p(9594) = ???

Good Luck!!

Corey

jhermiz

3564 Posts

Posted - 2004-07-16 : 11:19:48
Looking for the 9594th prime ?

Jon
www.web-impulse.com
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-07-16 : 11:20:55
Yep. Well, I'm not... you are !

Corey
Go to Top of Page

jhermiz

3564 Posts

Posted - 2004-07-16 : 11:24:12

p(n) = 1 + (sum from j=3 to n) ( (j-2)! - j[(j-2)!/j] )

Do you want a C++ solution ?
Wait till I get home :)...I love this type of stuff :)


Jon
www.web-impulse.com
Go to Top of Page

jhermiz

3564 Posts

Posted - 2004-07-16 : 11:25:23
O btw didnt Seive have an algorithm for this?
Will research though

Jon
www.web-impulse.com
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-07-16 : 11:30:02
Whats Seive??

btw - I'm a math major at clemson, so I also love this stuff

Corey
Go to Top of Page

jhermiz

3564 Posts

Posted - 2004-07-16 : 11:34:57
Here...
Solution is pretty easy with this:

http://www.nist.gov/dads/HTML/sieve.html



Jon
www.web-impulse.com
Go to Top of Page

jhermiz

3564 Posts

Posted - 2004-07-17 : 23:57:40
Sorry I'm late corey...been working long hours.

Here's a nice solution for your problem in C :)

http://www.thecenturoncompany.com/jhermiz/blog/nthprime.txt

Got any more ?

Jon
www.web-impulse.com

Can you dig it: http://www.thecenturoncompany.com/jhermiz/blog/
Go to Top of Page

kselvia
Aged Yak Warrior

526 Posts

Posted - 2004-07-18 : 01:04:06
I have a problem for you. It was a high school problem I had. Never found a real formula for it.

What is a 1 digit number where the numbers before it add up to the same thing as some number of numbers after it?

6 is the answer because 1+2+3+4+5 = 7+8 (15)

What is a 2 digit number? A 3 digit number?

Is there a formula to find these numbers or is the only way to find out to test every sumation of number sequences and see? (I know there is a formula for sums of sequences but is there a way to quickly identify these kinds of numbers.)

Can you generate the first 15 numbers that have this property?


--Ken
Your Kung-Fu is not strong. -- 'The Core'
Go to Top of Page

jhermiz

3564 Posts

Posted - 2004-07-18 : 02:29:11
quote:
Originally posted by kselvia

I have a problem for you. It was a high school problem I had. Never found a real formula for it.

What is a 1 digit number where the numbers before it add up to the same thing as some number of numbers after it?

6 is the answer because 1+2+3+4+5 = 7+8 (15)

What is a 2 digit number? A 3 digit number?

Is there a formula to find these numbers or is the only way to find out to test every sumation of number sequences and see? (I know there is a formula for sums of sequences but is there a way to quickly identify these kinds of numbers.)

Can you generate the first 15 numbers that have this property?


--Ken
Your Kung-Fu is not strong. -- 'The Core'



Hey Ken,

You cannot generalize something like this. You're just running into special cases such as 15=7+8. In fact there is too many inconsistances (5 digits on left, 2 on right to total 15, not good) to generalize this or prove it.

At least from my understanding I don't think I can generalize this.
Maybe you can / cannot but I don't see it.


Jon
www.web-impulse.com

Can you dig it: http://www.thecenturoncompany.com/jhermiz/blog/
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2004-07-18 : 03:27:39
Ah, so I CAN use a tally table for this one? <g> but I only get the first 5 as my tally table only goes up to 8,000 :-(

SELECT [Total before] = (S1.Number*(S1.Number+1))/2,
[Number] = S1.Number+1,
[Limit] = S1.Number+S2.Number+1
FROM Tally S1,
Tally S2
WHERE (S1.Number*(S1.Number+1))/2 = (S1.Number+1)*S2.Number+((S2.Number*(S2.Number+1))/2)
AND S2.Number <= S1.Number / 2 -- Prevent it taking forever!
AND S1.Number < 1000 -- Prevent it taking forever!
ORDER BY S1.Number

Kristen
Go to Top of Page

kselvia
Aged Yak Warrior

526 Posts

Posted - 2004-07-18 : 03:39:41
Sorry for hijacking your post Corey!

Good one Kristen. I was trying to do it myself but I was using a correlated subquery and couldn't figure it out. I wrote a BASIC program to solve it years ago and it took about a day to solve for a 5 digit number before the sum became larger than a double could hold. Interestingly, there was only 1 solution for each digit count.


--Ken
Your Kung-Fu is not strong. -- 'The Core'
Go to Top of Page

Arnold Fribble
Yak-finder General

1961 Posts

Posted - 2004-07-18 : 04:30:03
[code]
SELECT x, n
FROM (
SELECT n, CAST(FLOOR(SQRT(x2)) AS bigint) AS x, x2
FROM (
SELECT n, (CAST(n AS bigint)*n+n)/2 AS x2
FROM Numbers
WHERE n > 0
) AS A
) AS A
WHERE x*x = x2
ORDER BY x
[/code]

BTW, This link might be of interest
http://www.research.att.com/projects/OEIS?Anum=A001109
Go to Top of Page

kselvia
Aged Yak Warrior

526 Posts

Posted - 2004-07-18 : 04:51:24
I look at that and suppose you wrote it out just as it is, and well, that's just scary.

--Ken
Your Kung-Fu is not strong. -- 'The Core'
Go to Top of Page

Arnold Fribble
Yak-finder General

1961 Posts

Posted - 2004-07-18 : 05:16:09
Well, a few back of envelope scribblings, and an attack of conscience on the floating point accuracy...
But it's still a damned slow way: it turns out that there's a recurrence to calculate square triangular numbers!
Not very set-based, I'm afraid:

DECLARE @STn2 AS bigint, @STn1 AS bigint, @STn AS bigint
SET @STn2 = 0
SET @STn1 = 1
DECLARE @i AS int
SET @i = 0
WHILE @i < 12 BEGIN
SET @i = @i + 1
SET @STn = 34*@STn1 - @STn2 + 2
PRINT @STn
SET @STn2 = @STn1
SET @STn1 = @STn
END


Oh, better yet, there's a recurrence for the square root of that too:

DECLARE @a3 AS bigint, @a2 AS bigint, @a1 AS bigint, @a AS bigint
SET @a3 = 0
SET @a2 = 1
SET @a1 = 6
DECLARE @i AS int
SET @i = 0
WHILE @i < 23 BEGIN
SET @i = @i + 1
SET @a = 7*(@a1 - @a2) + @a3
PRINT @a
SET @a3 = @a2
SET @a2 = @a1
SET @a1 = @a
END


Much prettier in Haskell: lovely lazy evaluation (ideal for a lazy Sunday), infinite lists and proper bigints:

-- Square Triangular numbers
st = 0:1:(zipWith (\a b -> 34*a-b+2) (drop 1 st) st)
-- Their square roots (i.e. index in square numbers)
sts = 0:1:6:(zipWith3 (\a b c -> 7*(a-b)+c) (drop 2 sts) (drop 1 sts) sts)
-- index in triangular numbers
stt = 0:1:8:(zipWith3 (\a b c -> 7*(a-b)+c) (drop 2 stt) (drop 1 stt) stt)
-- put them all together:
stall = zip3 sts stt st
-- take the 10000th value
st10000 = stall !! 10000

Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-07-18 : 21:25:24
Sorry I disappeared, but I'm glad someone brought more fun to the table (thanx Ken!). I will have to try and decipher arnold's code... I will see if I can come up with a good puzzler...

Corey
Go to Top of Page

RyanRandall
Master Smack Fu Yak Hacker

1074 Posts

Posted - 2006-07-26 : 12:58:11
quote:
p(9594) = ???
I make it 100003

--calculation
select max(i) from (
select top 9594 i from (
select i from dbo.F_TABLE_PRIME(110000) union select 1) a
order by i) b
F_TABLE_PRIME is here:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=69646


Ryan Randall
www.monsoonmalabar.com London-based IT consultancy

Solutions are easy. Understanding the problem, now, that's the hard part.
Go to Top of Page

Arnold Fribble
Yak-finder General

1961 Posts

Posted - 2006-07-26 : 13:24:33
Yes, but what's the 10000th square triangular number?
Go to Top of Page

RyanRandall
Master Smack Fu Yak Hacker

1074 Posts

Posted - 2006-07-26 : 14:21:56

--calculation
declare @k int
set @k = 10000
select power((power(1 + sqrt(2.0), 2 * @k) - power(1 - sqrt(2.0), 2 * @k)), 2) / 32

/*result
1.53298e+15143
*/


Ryan Randall
www.monsoonmalabar.com London-based IT consultancy

Solutions are easy. Understanding the problem, now, that's the hard part.
Go to Top of Page

Arnold Fribble
Yak-finder General

1961 Posts

Posted - 2006-07-27 : 07:57:16
quote:
Originally posted by RyanRandall
1.53298e+15143



Strange, I make it
33286147720447497698418502916658933295106899800010553347187594203587936365096271
01982981756485007028041372678953700098486789857119482681149803651860170390906242
52378650269253595241447366824166243967706763869495049682473804757545109796673697
76701496041297827873732114916997497069191139723837890239819518406828153400205588
99238769195796271154799086566199043571056789668140208615893554458501430470861953
93132803138667656484358488158639873187287452502529278986006860683155514102906366
99135788419905179638957881868966016235061331880684725232567168906212388839473936
75996888276600940616204520108102791672921762617588996731631519643512394782135497
15621141839287921662132131988513927158923556115102408056902317833595534422118651
19363740502752036400032895278428354012336570915183714342892654347844795309830556
09636551652873795115915240506576284976312294566415707014154750204400276167211602
63882281283616054724712187743196515680252738109031960734796507838607568905936768
55556940643533987222101505535405746795412830454603954124747663008509568723842293
34949815673307933308151803374243909635345975934762443342337976784883108877469814
81173824011564508535311153218661351261568354652027049588533654024517858593769176
14162563653167659308167927531699020925216544734239164646612899520013016639278542
53261296407254745066924274604326770826445992236879889317634239290332014728187098
75816710575426856206329411547772027398981807073664257940004524505476558718562720
49090728337195153559240908934189655224192421622104394619958016715318970561968804
31283897905090455866575999086758896533376119532674500588269631496596097191818808
33723975756873216909434250875936947626037376864472340165608564561741609384264119
22621081379756746705814410885890474047767345003851465914372806609486822730166878
65864019043491892854237717297870642399101275831373540962338980723273435677407295
26111183394973321277351754811225641004947577001071850864970088689178044697019783
51088476103772438344012571401998685846836406193245684428104260670502399830304666
15329385984518504776880579641595478051785601394902206242835239170425856171591888
57874982071377465266779050762413878868322168867158855790301426853427487333342724
90541840175571439478637766939992326970498379105930340162372235008023431785523895
53991483392769238259163711172617306442333931684316763186347085603534403266516259
99040785002323425197830407369620117647268696018119422648499177120608331950562149
71928938986859213441876478496524106657925320907981666749024893550488469003282607
32418927644507288064208043935071937195695668589912614956536738501543773785977487
55711270443751815136170505386643736282367959898012057550918810807695523018547615
83529701844557248649952054621526264099649841523688165490027009525462757817260785
29848596680202687943217804252221765007692470132431343190422805355988979107459890
64340441732760020672397246886408723453534223730849522365660524325071520817129490
74460851890029976669700096391912811442928516544058526327695916182126641049145340
48929670377938241376437179335791985969838835238824580003498905244593099029298095
55932977508999962842635078621361847723182017950410817712425825384206062692834090
19673627111458976153010433851645530514723063181804408834780375111281483933300683
40437525432232355598641727278414110217255477021424390124348569028084396074252465
49724187828565417698059170716506070056876622060855497123052514236694668616204105
25857123188389940160879281731058797376116033838033164407171044073282976239170983
09958669722266335460942418791272800023511611055663994642489996068344758258624981
48450179523824067319907103582821774396292576456907526013460231433211588063102768
71341638737488469744807993561777971157062202691516762890328666116830318353675708
18020422762103982799657910146854831421739524894579401519778314006592883924509408
87616505449111586663001560992154592461671074113767657134741883182530875615756896
70048646379899454680301191213022091411973515451280804288025191409237871180521460
91291110395400773485101298969059060336394593549479163167078116293242739303540347
70842353838332364203928535050986565102365489595811928014294904007857510772542529
37628494563802215603002909130830810444638464392110854171793233415218182343946954
14286879994536330027476015067440747612930384968696614390086042003137553328024787
93599848508503822351432399315512510947562283739550109820530586048979359443747792
42025943303256135183337605957684759305243601570414670470440714969890182849723387
66408187426404213948611049119187745491884121294882785607330037077918838847192604
38700082160674828304208251355466131995943059399174333399193200467844131650006553
32541276243350766690491779529929994959222677455476470008626087891765825557452371
25950981654537719671705147701295041787057836025823168845531661266351192077979794
29959138552065806718911744653201267805068313919545320815046281050818308143023131
83923857741061856937202644705035123316728541221825716011682535849342097566875161
00030541538848352040506399413974651614739933966519480809184503638522872068599798
11992167009795664415935901738853916633803892167907959816417716117737815322443212
59192911273945437535210552718518043496592331588497578857798821686075090406045374
17967545225345254296006815814864794687822207480711625067856543641289889232841420
28360177108624006392245434292091557763984388717172126171860118994145135826321454
23554778662481119793351152350300800151084877824628950479755910463376330092133644
23421509632088524789355576051528189450652470844121018062986307461102534937701595
33647741317603148982210284854292791497263221923792847623617543977802986121511495
06482040088233444109145937472536377306558607940071030248640331292998414978766998
50449313728448863170265610988040423506103056300629790541996786664676658305586713
37712111247746444586228154366583657955422980625071328808638020968479824176218108
08894838288544679038911863567353887886097288295502238142101253630480349443139564
33227933456818513172991437767854981255174449378076952456429104848609385345297780
20707836179975149377007629608836318486885161480947812119068629439837163148494510
95561808141378442018959444786070051793720375646352512709621470677291083857190362
25613289537048228631564645127725843427814615384475239995503010872841925287760989
32246597298083902860096169281282171214297305686392745285859585241382798371707763
12628312062288424461867377938546700244424690514108060980819319825118484774841078
07894012683561657199848207438536377265458172204031745764119917732510710906832624
18935980329177432893324513786447538136250219700784353025654518926944034402108668
60737757038151140329473526968285679324838844098247601805684745139455274893222615
19971000986892257984119917504542924492737363618549859509611643949602438318230016
77452985271130260845880725747590778611392884711411966055414523416084662201964267
13487646931094941270852996215771515318431636469458746986446966677401548587212946
41156430972740543885697801093910712060206994643659462270112406615731213598512709
99724297158258380995722012378039480046298348593454705003049010084401737783135516
84383231109323375409147951426833695710268017775641629100419340655320860653175313
46885033749375931638293868347545554308902624902812395026556254094549243991825132
12989855554336424401539883194619401386962382033382813852254501205900952237283081
84782974272778183221644053469312926999591466070253720509283695907136412559309743
94711571708623088273898164200172448119416823109591664157068383181428492594426659
03846065070405536607225295821202939553249610590949228794095322108047818538626460
74402273961553943535225683264316359591260666883160353462875492914449255121104222
42277927937777541531891897560018336461612441350676538933453908083426416352378406
41075346887511117101690326274410099966706428154988009855602727357229560504611702
16545310461263961631115598453656526095411738662758718522447750620401254692691913
48432773467856279707427285693968873873996550266289537794465338723344356295765247
52219211774813130351129852720819408579893598821485374273444684540663613665385690
63488570588731924701442353566638532754158388456073748141088031015993458351696505
97595218556626462516968871757441873513429949194294783023272410749230020652709561
77855032139318096942607400583220881715401003345234738042947196691334394693822055
70775901197603827885822466829246262163105167093991648917977253596798573895604416
91810199068163456673994665465153564303393304685133522085547645244405057115088151
83274691409348215591261218055742914566293007038639027295602636865059097176930482
33588445935451521576989484852996371800632124224757577924800733903447225880910973
10842872203328368199948254704292312591874738565002227415524004719749041185695437
88842789770942576372530341935347944108460828326488031178829425399053734418980613
01148307378245383534142341634256234683687405980113755420717522574540828306761231
32874490687040208437870635924042045129285478451757719717151809243088345806428777
16543241815032662983075440333140775440916462780123057835835307545140498018995959
20557972053882505354074351015558906533867659445040286748329151043875976489924932
87653910005154902610342653813404982064248117967260936720930754168983139780168914
15508405722314993748413912169800793781121567782948111183967221782646831740868243
29151657023343302610281726173548542343479903514948938612904785364515068627927944
48269501595041453504183658881131321218551670602832756218714701715782350238463137
08309830186511392805599492685507118824371919267795413952788256189426888679141831
64543543015530466989447073783798523535980423396959716806846349584244227223688806
95385847310548543604696655801765853449334459882798931848002811848619350658264629
58729280756196406894724042600586611647662336131779704603458624936371559884467566
37793409495325929982647671728077502072605125558635799018374742761616106044038200
18973939274054686933600996028404134495818450992716576144449918970781772843716327
75047438885019984151623858058270142437592664264826261723173319724858413255001683
22087665497239707023661240173162207546716538192301980931830797972449713466226267
97874833096041918948616363635454841676074117604923939786757744278264824085819571
24058209560943420160172414324010903995571180947326038536598836309291644097983759
06455860130434091983231315158848776289193334072091383548406736693180125008715453
71096522702413993961384477305619458528843890562418246864799982042374752392077696
06349502690081600827089066946648461863528319396111008698200840271624551061118202
60482373958304862598303023110809581425749211355716949719475878542065312518107691
90866097159927948814950638760610158965750686706027336779427459082797198725970184
33688647688942025829695256794285732761033342732913996425704110049013634873459167
47392514935309613705770793247472669433751986351416078705879968427633786746128749
90519001299702915093565705481412164022790891387283635531873015350352113242011472
27763208415856139231110996242953364895242808702457771376729596675985024445536088
89994738727367237464975400428992059286342574756347451010617246466829680349093104
77443839917540250217245295726613951226777306769715472216616384663657249895820936
21181681521954860721218207856919644950621173043280496508613155641845243578650558
20622089366280103195389226966666171732695480082715389112509420215181874339546467
15334200937358578858637910300760244697728770762614374954079967194803469578684674
67021337594885429945939744203657220539910710757599276782877503298731242016328653
11332408626826187490260700172755184222805686838657966975764146434061864643573271
65202048162417260109937711458142962653133090690928309652225473019682932528844085
16385299228565015557620129579802026464880779732434767429192031761364850696102294
48166301230848756342858285077521881028616275491786412889253950757049276273534354
76421629556306775465212699428797495621256517694541577703580597065376439186676344
73148411149673504305200059218697538139222331577416428177498298199859820728020880
74833681646045651466472683652422635563744762979638071238865498618175264673471453
89517601072298245851005887108310410832151090014404811796213338602244375988900081
68971909221779176931998531403902980623464206911661636695556273547909485720082180
09568015176566687864887345798235906961213128819383740029820954150859416751982796
19767090263042035249513969426913224946048153187097523823291300056329238546482176
57354269365779145476858387158075392445510565617894453360204676572178292983390396
24495977578867120492416289265693825308750611173039647740826470956173644485966198
14155505687470012471767401448123828572900974508247375697414775315048385494541551
14229530145318288969265976412269766080667526136070118968868893565085715992554519
62942587153871651143111877157000332354429084065021419705898614373744323979801051
96190205490640918304840476581186738902033881506642922044576232657341613417938198
67431254108638689801260278098828513646930629731874577014216547027477692511763617
45522579683700965474584675279729811265443631499254481785510730540400858322710881
92207233876999240584465583311807592900417785671367967698247192055663965260556349
55975531813384334548837455221698642790887457146427041981508099972856313510524914
20342549778774959815229552710282563112958622833195755932753129190051385863844561
01740543290337936078427231189490954278662920819738593202019499165963202988662697
72870811629950777690928294723172934944508741924039232986292870332355444045690675
13480221756330568076063542208171053747497869362049212982196461462532224485387218
73577848158462697052217659871324100556609870510221640577271499919360143702582515
89590293855592427505359663625690200086083931760646310735815435793153557839341791
32853200165310360885237175220879501694715879960486860132173934551803037056954817
55529313528467847750589119947422907491829246984230328266375482268943800932356757
63929316240068374905697080256654948869801619766776912947694640180256993763514621
78382458481114326781419691422312355174265676520338306809267343998219579030334118
98865700203039427821875677628044769990736549568285327565059262452623273440989522
81291425037834501032117652274863460529998755777503250851338266025101194933135453
31058000844167854468120410238913621732532308822996826008947291999051791946643268
80392910733242760165383293902855494345534205866406921637660848324742269557936443
02929901117757132875946581362211320607572957630423158916580454900242869812928560
24432942013802898711016920019410197848089419108368721532143539938085569044215660
63389248941667657184708548248454817486513540326605126371021063814661344047750403
74793873456495171991354190933371203272665077047173642813321463626893236442419077
83900328196576980126885907683400573588411052957628997348738732815057852511872137
17225774047320306148075319290562638649588425992590993912633865906916986539132705
99620710482202443897858829187367565836567134090303034966741837914138119729208030
15897602645193249697618526772269757477975451627694110207132578271912514721697978
48888810482503805504890447531768220255898629270511653353246616025926558713632838
08568736072577667833466246677132216814058832592620184119404294395923132207827755
38436003397987041951634378811892735613570069774163872098789569601077863465946558
53042888646443929722994289438442173275580106189263288355717356805096299176049608
52634890130079335764513096468765071373434415830907631496278608902679726024098863
48640774979998550558314326473934040784395586282871796636741613179895455529367274
48724901321034129364285541479341267957672921126938308748592928688800007805084202
284469705014730954221493258496
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-07-27 : 08:57:54
It makes be feel so inadequate when you quote those things from memory Arnold
Go to Top of Page
    Next Page

- Advertisement -