pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Otniel Michael <otmic_ie@yahoo.com>
To: Aaron Bono <postgresql@aranya.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: About Div
Date: Tue, 25 Jul 2006 18:43:28 -0700 (PDT)
Message-ID: <20060726014328.38920.qmail@web53202.mail.yahoo.com> (raw)
In-Reply-To: <bf05e51c0607250800p44e98ce7uea04b19ff0cf8eee@mail.gmail.com>

Mr. Aaron. I am sorry, your solution didn't match in my case. 
Example for your solution :
A = 1
B = 1
C = 1
D = 1
E = 1
F = 1
G = 4

G have 4 candy. Its too much for G.

In my case, the solution is :
A = 1
B = 1
C = 1
D = 1
E = 2
F = 2
G = 2

The extra candy is given to three child.

Do you have the other solution? I need function in postgresql for my case.
Because my loop is too slow.

Btw thanks for your solution.

Aaron Bono <postgresql@aranya.com> wrote: On 7/25/06, Otniel Michael <otmic_ie@yahoo.com> wrote: Dear All,

I have a problem with this case :

I have 10 candy for 7 child (A, B, C, D, E, F, G).

Table X :
code   value
 -------  -------- 
 A        0        
 B        0        
 C        0         
 D        0        
 E        0        
 F        0        
 G       0        

And I want divide it with this algorithm : 
    A = 10 / 7 = 1
    B = (10-1) / (7-1)  = 9 / 6 = 1
    C = (10-2) / (7-2) = 8 / 5 = 1
    D = (10-3) / (7-3) = 7 / 4 = 1
    E = (10-4) / (7-4) = 6 / 3 = 2
    F = (10-6) / (7-5) = 4 / 2 = 2
    G = (10-8) / (7-6) = 2 / 1 = 2

In my first solution i use loop - for each record in my function. 
But it is too slow in a lot of data.
Did postgresql have a function for my case?

 
No loop necessary.  This is a simple math problem:

dividedamount := candy / childcount;
extra = candy % childcount; 

So the first (childcount - extra) get (dividedamount) pieces of candy and the last (extra) get (dividedamount + 1) pieces of candy.


==================================================================
    Aaron Bono
   Aranya Software Technologies, Inc.
   http://www.aranya.com
================================================================== 


-- 
 "He who is quick to become angry will commit folly, and a crafty man is hated" 
 

 __________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

view thread (5+ messages)  latest in thread

Message-ID: <20060726014328.38920.qmail@web53202.mail.yahoo.com>
Permalink:  ../20060726014328.38920.qmail@web53202.mail.yahoo.com/
Also on:    postgresql.org/message-id/20060726014328.38920.qmail@web53202.mail.yahoo.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: otmic_ie@yahoo.com, postgresql@aranya.com
  Subject: Re: About Div
  In-Reply-To: <20060726014328.38920.qmail@web53202.mail.yahoo.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox