pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedAbout Div
5+ messages / 3 participants
[nested] [flat]
* About Div
@ 2006-07-25 07:57 Otniel Michael <otmic_ie@yahoo.com>
2006-07-25 15:00 ` Re: About Div Aaron Bono <postgresql@aranya.com>
0 siblings, 1 reply; 5+ messages in thread
From: Otniel Michael @ 2006-07-25 07:57 UTC (permalink / raw)
To: pgsql-sql
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 / 2 = 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?
Thanks All.
--
"He who is quick to become angry will commit folly, and a crafty man is hated"
---------------------------------
Do you Yahoo!?
Everyone is raving about the all-new Yahoo! Mail Beta.
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: About Div
2006-07-25 07:57 About Div Otniel Michael <otmic_ie@yahoo.com>
@ 2006-07-25 15:00 ` Aaron Bono <postgresql@aranya.com>
2006-07-26 01:43 ` Re: About Div Otniel Michael <otmic_ie@yahoo.com>
0 siblings, 1 reply; 5+ messages in thread
From: Aaron Bono @ 2006-07-25 15:00 UTC (permalink / raw)
To: Otniel Michael <otmic_ie@yahoo.com>; +Cc: pgsql-sql
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 / 2 = 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
==================================================================
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: About Div
2006-07-25 07:57 About Div Otniel Michael <otmic_ie@yahoo.com>
2006-07-25 15:00 ` Re: About Div Aaron Bono <postgresql@aranya.com>
@ 2006-07-26 01:43 ` Otniel Michael <otmic_ie@yahoo.com>
2006-07-26 02:34 ` Re: About Div Ross Johnson <Ross.Johnson@homemail.com.au>
2006-07-26 14:49 ` Re: About Div Aaron Bono <postgresql@aranya.com>
0 siblings, 2 replies; 5+ messages in thread
From: Otniel Michael @ 2006-07-26 01:43 UTC (permalink / raw)
To: Aaron Bono <postgresql@aranya.com>; +Cc: pgsql-sql
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
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: About Div
2006-07-25 07:57 About Div Otniel Michael <otmic_ie@yahoo.com>
2006-07-25 15:00 ` Re: About Div Aaron Bono <postgresql@aranya.com>
2006-07-26 01:43 ` Re: About Div Otniel Michael <otmic_ie@yahoo.com>
@ 2006-07-26 02:34 ` Ross Johnson <Ross.Johnson@homemail.com.au>
1 sibling, 0 replies; 5+ messages in thread
From: Ross Johnson @ 2006-07-26 02:34 UTC (permalink / raw)
To: pgsql-sql
Otniel Michael wrote:
> 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.
Would this achieve the result you're looking for, or must it be done
with a function?
Assuming you already know the value of totalchildren and totalcandy:
BEGIN
-- Everyone gets at least this number, which could be zero or more.
UPDATE X SET value = (totalcandy / totalchildren);
-- Hand the remainder out one at a time until all are gone.
UPDATE X SET value = (value + 1)
WHERE code = (SELECT code FROM X ORDER BY code DESC LIMIT (totalcandy
% totalchildren));
COMMIT
Ross
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: About Div
2006-07-25 07:57 About Div Otniel Michael <otmic_ie@yahoo.com>
2006-07-25 15:00 ` Re: About Div Aaron Bono <postgresql@aranya.com>
2006-07-26 01:43 ` Re: About Div Otniel Michael <otmic_ie@yahoo.com>
@ 2006-07-26 14:49 ` Aaron Bono <postgresql@aranya.com>
1 sibling, 0 replies; 5+ messages in thread
From: Aaron Bono @ 2006-07-26 14:49 UTC (permalink / raw)
To: Otniel Michael <otmic_ie@yahoo.com>; +Cc: pgsql-sql
On 7/25/06, Otniel Michael <otmic_ie@yahoo.com> wrote:
>
> 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.
>
I think you misunderstood how to use the MOD value. 10 % 7 = 3 so the last
3 people get 1 extra, NOT the last person getting the extra 3.
But it looks like Ross got you the code to fix the problem. Check what he
provided.
==================================================================
Aaron Bono
Aranya Software Technologies, Inc.
http://www.aranya.com
==================================================================
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2006-07-26 14:49 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-07-25 07:57 About Div Otniel Michael <otmic_ie@yahoo.com>
2006-07-25 15:00 ` Aaron Bono <postgresql@aranya.com>
2006-07-26 01:43 ` Otniel Michael <otmic_ie@yahoo.com>
2006-07-26 02:34 ` Ross Johnson <Ross.Johnson@homemail.com.au>
2006-07-26 14:49 ` Aaron Bono <postgresql@aranya.com>
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