<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: How Many Ways to Solve this Problem?  Add the Sequential Numbers x Through y</title>
	<atom:link href="http://hoopercharles.wordpress.com/2011/07/13/how-many-ways-to-solve-this-problem-add-the-sequential-numbers-x-through-y/feed/" rel="self" type="application/rss+xml" />
	<link>http://hoopercharles.wordpress.com/2011/07/13/how-many-ways-to-solve-this-problem-add-the-sequential-numbers-x-through-y/</link>
	<description>Miscellaneous Random Oracle Topics: Stop, Think, ... Understand</description>
	<lastBuildDate>Mon, 13 May 2013 14:10:06 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Charles Hooper</title>
		<link>http://hoopercharles.wordpress.com/2011/07/13/how-many-ways-to-solve-this-problem-add-the-sequential-numbers-x-through-y/#comment-3775</link>
		<dc:creator><![CDATA[Charles Hooper]]></dc:creator>
		<pubDate>Wed, 10 Aug 2011 14:56:57 +0000</pubDate>
		<guid isPermaLink="false">http://hoopercharles.wordpress.com/?p=5144#comment-3775</guid>
		<description><![CDATA[Gary,

I appologize in advance if I am misunderstanding your comment - I think that you are asking how to fix the second SQL statement that you posted.

In the blog article, the following formula appeared in white letters (almost invisible):
&lt;pre&gt;
(max - min + 1) / 2 * (min + max)
&lt;/pre&gt;

That formula is logically equivalent to this one:
&lt;pre&gt;
(max + min) / 2 * (max - min + 1)
&lt;/pre&gt;

And the above formula is logically equivalent to the first SQL statement that you provided.

My sample SQL statement:
&lt;pre&gt;
SELECT   (105 - 6 + 1) /2 * (6 + 105) FROM   DUAL;
&lt;/pre&gt;

Your SQL statement with the same input numbers:
&lt;pre&gt;
SELECT (105 + 6) / 2 * 100 FROM DUAL;
&lt;/pre&gt;

A minor adjustment to your second SQL statement:
&lt;pre&gt;
SELECT
  (MAX(N) + MIN(N)) / 2 * COUNT(N) S
FROM
  (SELECT
    LEVEL + 5 N
  FROM
    DUAL
  CONNECT BY
    LEVEL&lt;=((105 - 6) + 1));
 
         S
----------
      5550
&lt;/pre&gt;]]></description>
		<content:encoded><![CDATA[<p>Gary,</p>
<p>I appologize in advance if I am misunderstanding your comment &#8211; I think that you are asking how to fix the second SQL statement that you posted.</p>
<p>In the blog article, the following formula appeared in white letters (almost invisible):</p>
<pre>
(max - min + 1) / 2 * (min + max)
</pre>
<p>That formula is logically equivalent to this one:</p>
<pre>
(max + min) / 2 * (max - min + 1)
</pre>
<p>And the above formula is logically equivalent to the first SQL statement that you provided.</p>
<p>My sample SQL statement:</p>
<pre>
SELECT   (105 - 6 + 1) /2 * (6 + 105) FROM   DUAL;
</pre>
<p>Your SQL statement with the same input numbers:</p>
<pre>
SELECT (105 + 6) / 2 * 100 FROM DUAL;
</pre>
<p>A minor adjustment to your second SQL statement:</p>
<pre>
SELECT
  (MAX(N) + MIN(N)) / 2 * COUNT(N) S
FROM
  (SELECT
    LEVEL + 5 N
  FROM
    DUAL
  CONNECT BY
    LEVEL&lt;=((105 - 6) + 1));
 
         S
----------
      5550
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gary Colbran</title>
		<link>http://hoopercharles.wordpress.com/2011/07/13/how-many-ways-to-solve-this-problem-add-the-sequential-numbers-x-through-y/#comment-3774</link>
		<dc:creator><![CDATA[Gary Colbran]]></dc:creator>
		<pubDate>Wed, 10 Aug 2011 12:40:27 +0000</pubDate>
		<guid isPermaLink="false">http://hoopercharles.wordpress.com/?p=5144#comment-3774</guid>
		<description><![CDATA[What&#039;s wrong with this ?

select (max(n)+min(n))/2*count(n) from (select level n from dual connect by level &lt; 99);

Should work for any starting number, amount of numbers or increment.

select (max(n)+min(n))/2*count(n) from (select (level+x)*i from dual connect by level &lt; y);]]></description>
		<content:encoded><![CDATA[<p>What&#8217;s wrong with this ?</p>
<p>select (max(n)+min(n))/2*count(n) from (select level n from dual connect by level &lt; 99);</p>
<p>Should work for any starting number, amount of numbers or increment.</p>
<p>select (max(n)+min(n))/2*count(n) from (select (level+x)*i from dual connect by level &lt; y);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Charles Hooper</title>
		<link>http://hoopercharles.wordpress.com/2011/07/13/how-many-ways-to-solve-this-problem-add-the-sequential-numbers-x-through-y/#comment-3666</link>
		<dc:creator><![CDATA[Charles Hooper]]></dc:creator>
		<pubDate>Thu, 14 Jul 2011 18:57:31 +0000</pubDate>
		<guid isPermaLink="false">http://hoopercharles.wordpress.com/?p=5144#comment-3666</guid>
		<description><![CDATA[Enrique, Radoslav, Joel good points.  

I was actually thinking something similar to what Enrique posted when I added the comment about a simple PL/SQL loop - it probably is not a good idea from a performance perspective to create a PL/SQL function to find the sum of the numbers from 1 to 100 (or x to y) and then return that value in a SQL statement.  As Joel mentioned, the performance issues due to the unnecessary context switches can be problematic.

It might not have been 100% clear, but this blog post was looking for solutions to the problem that used Oracle Database to help find the answer to the problem.  This time there was no restriction stating that the answer had to be retrieved with a SQL select statement.  If you encountered the problem of having to calculate the sum of the numbers between x and y, and were in the middle of programming a PL/SQL function, it is more efficient to find the answer using just a PL/SQL FOR loop (or better yet just calculate the answer using the formulas provided in the comments) than it would be to incur a context switch and execute a SQL statement to calculate the answer.  I was a bit surprised when I saw Radoslav&#039;s comment above - yet he has a valid point that if the answer is needed as part of a PL/SQL routine, calculating the answer in that routine is more efficient.

Technically, the question posed by this blog article also did not restrict the use of Java to help calculate the answer, so there might be a couple of additional solutions.]]></description>
		<content:encoded><![CDATA[<p>Enrique, Radoslav, Joel good points.  </p>
<p>I was actually thinking something similar to what Enrique posted when I added the comment about a simple PL/SQL loop &#8211; it probably is not a good idea from a performance perspective to create a PL/SQL function to find the sum of the numbers from 1 to 100 (or x to y) and then return that value in a SQL statement.  As Joel mentioned, the performance issues due to the unnecessary context switches can be problematic.</p>
<p>It might not have been 100% clear, but this blog post was looking for solutions to the problem that used Oracle Database to help find the answer to the problem.  This time there was no restriction stating that the answer had to be retrieved with a SQL select statement.  If you encountered the problem of having to calculate the sum of the numbers between x and y, and were in the middle of programming a PL/SQL function, it is more efficient to find the answer using just a PL/SQL FOR loop (or better yet just calculate the answer using the formulas provided in the comments) than it would be to incur a context switch and execute a SQL statement to calculate the answer.  I was a bit surprised when I saw Radoslav&#8217;s comment above &#8211; yet he has a valid point that if the answer is needed as part of a PL/SQL routine, calculating the answer in that routine is more efficient.</p>
<p>Technically, the question posed by this blog article also did not restrict the use of Java to help calculate the answer, so there might be a couple of additional solutions.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Raj</title>
		<link>http://hoopercharles.wordpress.com/2011/07/13/how-many-ways-to-solve-this-problem-add-the-sequential-numbers-x-through-y/#comment-3665</link>
		<dc:creator><![CDATA[Raj]]></dc:creator>
		<pubDate>Thu, 14 Jul 2011 08:11:54 +0000</pubDate>
		<guid isPermaLink="false">http://hoopercharles.wordpress.com/?p=5144#comment-3665</guid>
		<description><![CDATA[That&#039;s correct.  It was included just for my testing purpose.

Thanks]]></description>
		<content:encoded><![CDATA[<p>That&#8217;s correct.  It was included just for my testing purpose.</p>
<p>Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Radoslav Golian</title>
		<link>http://hoopercharles.wordpress.com/2011/07/13/how-many-ways-to-solve-this-problem-add-the-sequential-numbers-x-through-y/#comment-3663</link>
		<dc:creator><![CDATA[Radoslav Golian]]></dc:creator>
		<pubDate>Wed, 13 Jul 2011 21:06:39 +0000</pubDate>
		<guid isPermaLink="false">http://hoopercharles.wordpress.com/?p=5144#comment-3663</guid>
		<description><![CDATA[I didn&#039;t notice that Mark was faster than me :)]]></description>
		<content:encoded><![CDATA[<p>I didn&#8217;t notice that Mark was faster than me <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Radoslav Golian</title>
		<link>http://hoopercharles.wordpress.com/2011/07/13/how-many-ways-to-solve-this-problem-add-the-sequential-numbers-x-through-y/#comment-3662</link>
		<dc:creator><![CDATA[Radoslav Golian]]></dc:creator>
		<pubDate>Wed, 13 Jul 2011 20:42:15 +0000</pubDate>
		<guid isPermaLink="false">http://hoopercharles.wordpress.com/?p=5144#comment-3662</guid>
		<description><![CDATA[Optimal solution is to find a closed form for that sum and use it as an algorithm.. 
Finding a closed form for that easy sum is trivial (http://www.jimloy.com/algebra/gauss.htm)

[code]
sum{i=x..y} (i) = x + (x + 1) + (x + 2) + .. + (y - 2) + (y - 1) + y = (x + y) + (x + 1 + y - 1) + (x + 2 + y - 2) .... =  (x + y) * ((y - x + 1)) / 2 

so the best solution is:

var x number;
var y number;
var result number;

exec :X  := &amp;x;
exec :y := &amp;y;

begin
   :result := (:X + :y) * (:y - :X  + 1) / 2;
end;
[/code]

any SQL statement would be slower..

&lt;em&gt;(edit CH: fixed the scrambled code by replacing x with X in the code section)&lt;/em&gt;]]></description>
		<content:encoded><![CDATA[<p>Optimal solution is to find a closed form for that sum and use it as an algorithm..<br />
Finding a closed form for that easy sum is trivial (<a href="http://www.jimloy.com/algebra/gauss.htm" rel="nofollow">http://www.jimloy.com/algebra/gauss.htm</a>)</p>
<pre class="brush: plain; title: ; notranslate">
sum{i=x..y} (i) = x + (x + 1) + (x + 2) + .. + (y - 2) + (y - 1) + y = (x + y) + (x + 1 + y - 1) + (x + 2 + y - 2) .... =  (x + y) * ((y - x + 1)) / 2 

so the best solution is:

var x number;
var y number;
var result number;

exec :X  := &amp;x;
exec :y := &amp;y;

begin
   :result := (:X + :y) * (:y - :X  + 1) / 2;
end;
</pre>
<p>any SQL statement would be slower..</p>
<p><em>(edit CH: fixed the scrambled code by replacing x with X in the code section)</em></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jgarry</title>
		<link>http://hoopercharles.wordpress.com/2011/07/13/how-many-ways-to-solve-this-problem-add-the-sequential-numbers-x-through-y/#comment-3661</link>
		<dc:creator><![CDATA[jgarry]]></dc:creator>
		<pubDate>Wed, 13 Jul 2011 20:22:47 +0000</pubDate>
		<guid isPermaLink="false">http://hoopercharles.wordpress.com/?p=5144#comment-3661</guid>
		<description><![CDATA[As long as you don&#039;t get tripped up by those darn context switches...]]></description>
		<content:encoded><![CDATA[<p>As long as you don&#8217;t get tripped up by those darn context switches&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Charles Hooper</title>
		<link>http://hoopercharles.wordpress.com/2011/07/13/how-many-ways-to-solve-this-problem-add-the-sequential-numbers-x-through-y/#comment-3660</link>
		<dc:creator><![CDATA[Charles Hooper]]></dc:creator>
		<pubDate>Wed, 13 Jul 2011 19:49:12 +0000</pubDate>
		<guid isPermaLink="false">http://hoopercharles.wordpress.com/?p=5144#comment-3660</guid>
		<description><![CDATA[Enrique,

The first solution that I thought about is similar to what you posted, and then I started wondering... what if the lower number is not 1, but instead some other number.  I abandoned my solution that was similar to your solution before sliding the CONNECT BY portion of the query into an inline view as you did:
&lt;pre&gt;
VARIABLE X NUMBER
VARIABLE Y NUMBER
EXEC :X := 6
EXEC :Y := 105
 
select sum(r)
from (select rownum r from dual connect by level &lt;= :y)
where r between :X and :y;
 
    SUM(R)
----------
      5550
&lt;/pre&gt;

&lt;pre&gt;
EXEC :Y := 104
 
select sum(r)
from (select rownum r from dual connect by level &lt;= :y)
where r between :X and :y;
 
    SUM(R)
----------
      5445
&lt;/pre&gt;

Those are the expected values.]]></description>
		<content:encoded><![CDATA[<p>Enrique,</p>
<p>The first solution that I thought about is similar to what you posted, and then I started wondering&#8230; what if the lower number is not 1, but instead some other number.  I abandoned my solution that was similar to your solution before sliding the CONNECT BY portion of the query into an inline view as you did:</p>
<pre>
VARIABLE X NUMBER
VARIABLE Y NUMBER
EXEC :X := 6
EXEC :Y := 105
 
select sum(r)
from (select rownum r from dual connect by level &lt;= :y)
where r between :X and :y;
 
    SUM(R)
----------
      5550
</pre>
<pre>
EXEC :Y := 104
 
select sum(r)
from (select rownum r from dual connect by level &lt;= :y)
where r between :X and :y;
 
    SUM(R)
----------
      5445
</pre>
<p>Those are the expected values.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Charles Hooper</title>
		<link>http://hoopercharles.wordpress.com/2011/07/13/how-many-ways-to-solve-this-problem-add-the-sequential-numbers-x-through-y/#comment-3659</link>
		<dc:creator><![CDATA[Charles Hooper]]></dc:creator>
		<pubDate>Wed, 13 Jul 2011 19:32:41 +0000</pubDate>
		<guid isPermaLink="false">http://hoopercharles.wordpress.com/?p=5144#comment-3659</guid>
		<description><![CDATA[Dom,

Interesting solutions.  I had a bit of trouble generalizing the method using the MODEL clause so that it would not add the numbers 1 through x-1:
&lt;pre&gt;
select xmlquery((listagg(x,&#039; + &#039; ) within group (order by x)) returning content).getnumberval() tot  
from (select rownum x  
        from   dual  
      connect by rownum &lt;= 104)  
where x &gt;= 6;  
  
       TOT
----------
      5445
&lt;/pre&gt;
 
&lt;pre&gt;
with xyz (x,y) as
  (select 6 x, 6 y from dual
   union all
   select x+1       as x
   ,      y + (x+1) as y
   from   xyz
   where  x &lt;= 104)
 select y
 from   xyz
 where  x = 104;
 
         Y
----------
      5445
&lt;/pre&gt;

&lt;pre&gt;
select y
 from   dual
 model
 dimension by (0 x)
 measures     (0 y)
 rules     iterate (4294967295) until (iteration_number = 104)
              (y[0] = nvl(y[0],6)+iteration_number);
 
         Y
----------
      5460 
&lt;/pre&gt;

Can you see what I did wrong?]]></description>
		<content:encoded><![CDATA[<p>Dom,</p>
<p>Interesting solutions.  I had a bit of trouble generalizing the method using the MODEL clause so that it would not add the numbers 1 through x-1:</p>
<pre>
select xmlquery((listagg(x,' + ' ) within group (order by x)) returning content).getnumberval() tot  
from (select rownum x  
        from   dual  
      connect by rownum &lt;= 104)  
where x &gt;= 6;  
  
       TOT
----------
      5445
</pre>
<pre>
with xyz (x,y) as
  (select 6 x, 6 y from dual
   union all
   select x+1       as x
   ,      y + (x+1) as y
   from   xyz
   where  x &lt;= 104)
 select y
 from   xyz
 where  x = 104;
 
         Y
----------
      5445
</pre>
<pre>
select y
 from   dual
 model
 dimension by (0 x)
 measures     (0 y)
 rules     iterate (4294967295) until (iteration_number = 104)
              (y[0] = nvl(y[0],6)+iteration_number);
 
         Y
----------
      5460 
</pre>
<p>Can you see what I did wrong?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Radoslav Golian</title>
		<link>http://hoopercharles.wordpress.com/2011/07/13/how-many-ways-to-solve-this-problem-add-the-sequential-numbers-x-through-y/#comment-3658</link>
		<dc:creator><![CDATA[Radoslav Golian]]></dc:creator>
		<pubDate>Wed, 13 Jul 2011 19:29:35 +0000</pubDate>
		<guid isPermaLink="false">http://hoopercharles.wordpress.com/?p=5144#comment-3658</guid>
		<description><![CDATA[because PL/SQL (without SQL) is faster ;)]]></description>
		<content:encoded><![CDATA[<p>because PL/SQL (without SQL) is faster <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
</channel>
</rss>
