Greetings, I have encountered a problem which is starting to stretch the limits of my current abilities in an area of TSQL and have decided to post it here with a request for assistance. Essentially over time if it is possible I need to translate a fair number of maple algorithms (see below to TSQL). These algorithms all involve combinatorial mathematics. If I can get the hang of one the rest should follow. So below I have presented an example. Thanks for any assistance provided. PROBLEM 1: No of Ordered factorizations of n ================================== Background URL: http://www.research.att.com/projects/OEIS?Anum=A074206 *Sequence: 0,1,1,1,2,1,3,1,4,2,3,1,8,1,3,3,8,1,8,1,8,3,3,1,20,2,3,4,8, 1,13,1,16,3,3,3,26,1,3,3,20,1,13,1,8,8,3,1,48,2,8,3,8,1,20, 3,20,3,3,1,44,1,3,8,32,3,13,1,8,3,13,1,76,1,3,8,8,3,13,1,48, 8,3,1,44,3,3,3,20,1,44,3,8,3,3,3,112 Name: Number of ordered factorizations of n. References L. Comtet, Advanced Combinatorics, Reidel, 1974, p. 126, see #27. R. Honsberger, Mathematical Gems III, M.A.A., 1985, p. 141. J. Riordan, An Introduction to Combinatorial Analysis, Wiley, 1958, Links: E. W. Weisstein, Link to a section of The World of Mathematics. Index entries for "core" sequences Eric Weisstein's World of Mathematics, Ordered Factorization Formula: With different offset: a(n) = sum of all a(i) such that i divides n and i < n (Clark Kimberling). a(p^k)=2^(k-1). Dirichlet g.f.: 1/(2-zeta(s)). - Herb Wilf, Apr 29, 2003 The Riemann zeta function looks a little foreboding for TSQL (an aside .... is it?) and there are always sample maple routines provided, such as the following: Maple: a:=array(1..150): for k from 1 to 150 do a[k]:=0 od: a[1]:=1: for j from 2 to 150 do for m from 1 to j-1 do if j mod m = 0 then a[j]:=a[j]+a[m] fi: od: od: for k from 1 to 150 do printf(`%d,`,a[k]) What the above routine does is generate, for the numbers 2,3,4 ...150 the corresponding count of ordered factor sequences as defined above.* Example: Number of ordered factorizations of 8 is 4: 8 = 2*4 = 4*2 = 2*2*2. I recall writing a similar bit of code in basic 20 years ago, and it used arrays. Not sure how this might be implemented with TSQL. Can anyone assist here? Many thanks!! -- Pete Brown Winluck P/L IT Managers & Engineers Falls Creek Australia www.mountainman.com.au/software
The script below works with SQL Server 2000. You can substitute a temp table for the table variable if you are using an earlier Microsoft SQL Server version. SET NOCOUNT ON DECLARE @k int, @j int, @m int DECLARE @a TABLE(Ordinal int, Value int) SET @k = 0 WHILE @k < 151 BEGIN INSERT INTO @a VALUES(@k, 0) SET @k = @k + 1 END SET @k = 1 WHILE @k < 2 BEGIN UPDATE @a SET Value = 1 WHERE Ordinal = 1 SET @j = 2 WHILE @j < 151 BEGIN SET @m = 1 WHILE @m < @j BEGIN IF @j % @m = 0 UPDATE @a SET Value = Value + (SELECT Value FROM @a WHERE Ordinal = @m) WHERE Ordinal = @j SET @m = @m + 1 END SET @j = @j + 1 END SET @k = @k + 1 END SELECT Ordinal AS n, Value as "a(n)" FROM @a -- Hope this helps. Dan Guzman SQL Server MVP [quoted text, click to view] "mountain man" <hobbit@southern_seaweed.com.op> wrote in message news:GIV6c.116370$Wa.77309@news-server.bigpond.net.au... > Greetings, > > I have encountered a problem which is starting to stretch > the limits of my current abilities in an area of TSQL and have > decided to post it here with a request for assistance. > > Essentially over time if it is possible I need to translate a > fair number of maple algorithms (see below to TSQL). > > These algorithms all involve combinatorial mathematics. > If I can get the hang of one the rest should follow. > So below I have presented an example. > Thanks for any assistance provided. > > > PROBLEM 1: No of Ordered factorizations of n > ================================== > Background URL: > http://www.research.att.com/projects/OEIS?Anum=A074206 > > *Sequence: 0,1,1,1,2,1,3,1,4,2,3,1,8,1,3,3,8,1,8,1,8,3,3,1,20,2,3,4,8, > 1,13,1,16,3,3,3,26,1,3,3,20,1,13,1,8,8,3,1,48,2,8,3,8,1,20, > 3,20,3,3,1,44,1,3,8,32,3,13,1,8,3,13,1,76,1,3,8,8,3,13,1,48, > 8,3,1,44,3,3,3,20,1,44,3,8,3,3,3,112 > Name: Number of ordered factorizations of n. > References L. Comtet, Advanced Combinatorics, Reidel, 1974, p. 126, see #27. > R. Honsberger, Mathematical Gems III, M.A.A., 1985, p. 141. > J. Riordan, An Introduction to Combinatorial Analysis, Wiley, > 1958, > Links: E. W. Weisstein, Link to a section of The World of Mathematics. > Index entries for "core" sequences > Eric Weisstein's World of Mathematics, Ordered Factorization > > Formula: With different offset: a(n) = sum of all a(i) > such that i divides n and i < n (Clark Kimberling). > a(p^k)=2^(k-1). > Dirichlet g.f.: 1/(2-zeta(s)). - Herb Wilf, Apr 29, 2003 > > The Riemann zeta function looks a little foreboding for TSQL > (an aside .... is it?) and there are always sample maple routines > provided, such as the following: > > Maple: a:=array(1..150): for k from 1 to 150 do a[k]:=0 od: a[1]:=1: for > j from 2 > to 150 do for m from 1 to j-1 do if j mod m = 0 then > a[j]:=a[j]+a[m] fi: od: od: > for k from 1 to 150 do printf(`%d,`,a[k]) > > What the above routine does is generate, for the numbers 2,3,4 ...150 > the corresponding count of ordered factor sequences as defined above.* > > Example: Number of ordered factorizations of 8 is 4: > 8 = 2*4 = 4*2 = 2*2*2. > > I recall writing a similar bit of code in basic 20 years ago, and it used > arrays. Not sure how this might be implemented with TSQL. > > Can anyone assist here? > Many thanks!! > > > > > -- > Pete Brown > Winluck P/L > IT Managers & Engineers > Falls Creek > Australia > www.mountainman.com.au/software > > > > > > > > > >
Thanks very much Dan Guzman. I will study the structure of the code and reverse engineer the others. Best wishes for now, Pete Brown Falls Creek, Oz [quoted text, click to view] "Dan Guzman" <danguzman@nospam-earthlink.net> wrote in message news:45i7c.31783$%06.8596@newsread2.news.pas.earthlink.net... > The script below works with SQL Server 2000. You can substitute a temp > table for the table variable if you are using an earlier Microsoft SQL > Server version. > > SET NOCOUNT ON > DECLARE @k int, @j int, @m int > DECLARE @a TABLE(Ordinal int, Value int) > SET @k = 0 > WHILE @k < 151 > BEGIN > INSERT INTO @a VALUES(@k, 0) > SET @k = @k + 1 > END > SET @k = 1 > WHILE @k < 2 > BEGIN > UPDATE @a SET Value = 1 WHERE Ordinal = 1 > SET @j = 2 > WHILE @j < 151 > BEGIN > SET @m = 1 > WHILE @m < @j > BEGIN > IF @j % @m = 0 > UPDATE @a > SET Value = Value + (SELECT Value FROM @a WHERE Ordinal = @m) > WHERE Ordinal = @j > SET @m = @m + 1 > END > SET @j = @j + 1 > END > SET @k = @k + 1 > END > > SELECT > Ordinal AS n, > Value as "a(n)" > FROM @a > > > -- > Hope this helps. > > Dan Guzman > SQL Server MVP > > "mountain man" <hobbit@southern_seaweed.com.op> wrote in message > news:GIV6c.116370$Wa.77309@news-server.bigpond.net.au... > > Greetings, > > > > I have encountered a problem which is starting to stretch > > the limits of my current abilities in an area of TSQL and have > > decided to post it here with a request for assistance. > > > > Essentially over time if it is possible I need to translate a > > fair number of maple algorithms (see below to TSQL). > > > > These algorithms all involve combinatorial mathematics. > > If I can get the hang of one the rest should follow. > > So below I have presented an example. > > Thanks for any assistance provided. > > > > > > PROBLEM 1: No of Ordered factorizations of n > > ================================== > > Background URL: > > http://www.research.att.com/projects/OEIS?Anum=A074206 > > > > *Sequence: 0,1,1,1,2,1,3,1,4,2,3,1,8,1,3,3,8,1,8,1,8,3,3,1,20,2,3,4,8, > > 1,13,1,16,3,3,3,26,1,3,3,20,1,13,1,8,8,3,1,48,2,8,3,8,1,20, > > 3,20,3,3,1,44,1,3,8,32,3,13,1,8,3,13,1,76,1,3,8,8,3,13,1,48, > > 8,3,1,44,3,3,3,20,1,44,3,8,3,3,3,112 > > Name: Number of ordered factorizations of n. > > References L. Comtet, Advanced Combinatorics, Reidel, 1974, p. 126, see > #27. > > R. Honsberger, Mathematical Gems III, M.A.A., 1985, p. 141. > > J. Riordan, An Introduction to Combinatorial Analysis, Wiley, > > 1958, > > Links: E. W. Weisstein, Link to a section of The World of Mathematics. > > Index entries for "core" sequences > > Eric Weisstein's World of Mathematics, Ordered Factorization > > > > Formula: With different offset: a(n) = sum of all a(i) > > such that i divides n and i < n (Clark Kimberling). > > a(p^k)=2^(k-1). > > Dirichlet g.f.: 1/(2-zeta(s)). - Herb Wilf, Apr 29, 2003 > > > > The Riemann zeta function looks a little foreboding for TSQL > > (an aside .... is it?) and there are always sample maple routines > > provided, such as the following: > > > > Maple: a:=array(1..150): for k from 1 to 150 do a[k]:=0 od: a[1]:=1: > for > > j from 2 > > to 150 do for m from 1 to j-1 do if j mod m = 0 then > > a[j]:=a[j]+a[m] fi: od: od: > > for k from 1 to 150 do printf(`%d,`,a[k]) > > > > What the above routine does is generate, for the numbers 2,3,4 ...150 > > the corresponding count of ordered factor sequences as defined above.* > > > > Example: Number of ordered factorizations of 8 is 4: > > 8 = 2*4 = 4*2 = 2*2*2. > > > > I recall writing a similar bit of code in basic 20 years ago, and it used > > arrays. Not sure how this might be implemented with TSQL. > > > > Can anyone assist here? > > Many thanks!! > > > > > > > > > > -- > > Pete Brown > > Winluck P/L > > IT Managers & Engineers > > Falls Creek > > Australia > > www.mountainman.com.au/software > > > > > > > > > > > > > > > > > > > > > >
Don't see what you're looking for? Try a search.
|