From: Dan Joseph on
On Thu, Apr 22, 2010 at 12:16 PM, Richard Quadling <rquadling(a)googlemail.com
> wrote:

> On 22 April 2010 14:48, Dan Joseph <dmjoseph(a)gmail.com> wrote:
> This seems to be working ...
>
> <?php
> function findBestFactors($Value, $GroupSize, array &$Factors = null)
> {
> $Factors = array();
> foreach(range(1, ceil(sqrt($Value))) as $Factor)
> {
> if (0 == ($Value % $Factor))
> {
> if ($Factor <= $GroupSize)
> {
> $Factors[] = $Factor;
> }
> if ($Factor != ($OtherFactor = ($Value / $Factor))
> && $OtherFactor
> <= $GroupSize)
> {
> $Factors[] = $OtherFactor;
> }
> }
>
> if ($Factor >= $GroupSize)
> {
> break;
> }
> }
>
> rsort($Factors);
>
> return reset($Factors);
> }
>
> echo findBestFactors($argv[1], $argv[2], $Factors), PHP_EOL;
> ?>
>
>
> factors 1252398988 5000
>
> outputs ...
>
> 4882
>
> and 21 for your value 1252398
>
>
>
Wow! thanks... I just plopped it into phped and fired off some tests, and
I agree, seems to work fine. I appreciate your help today. I am still
looking over the algebra stuff, and am now comparing it to your code. This
will get me moving forward better in my project. Thank you!

--
-Dan Joseph

www.canishosting.com - Unlimited Hosting Plans start @ $3.95/month. Promo
Code "NEWTHINGS" for 10% off initial order

http://www.facebook.com/canishosting
http://www.facebook.com/originalpoetry
From: Richard Quadling on
On 22 April 2010 17:47, Developer Team <dev(a)thebat.net> wrote:
> Awesome source.
> Thanks
>
> On 4/22/10, Richard Quadling <rquadling(a)googlemail.com> wrote:
>> On 22 April 2010 14:48, Dan Joseph <dmjoseph(a)gmail.com> wrote:
>>> On Thu, Apr 22, 2010 at 10:29 AM, Richard Quadling
>>> <rquadling(a)googlemail.com
>>>> wrote:
>>>
>>>>  >
>>>> > It sounds like you are looking for factors.
>>>> >
>>>> >
>>>> http://www.algebra.com/algebra/homework/divisibility/factor-any-number-1.solver
>>>> >
>>>> > Solution by Find factors of any number
>>>> >
>>>> > 1252398 is NOT a prime number: 1252398 = 2 * 3 * 7 * 29819
>>>> > Work Shown
>>>> >
>>>> > 1252398 is divisible by 2: 1252398 = 626199 * 2.
>>>> > 626199 is divisible by 3: 626199 = 208733 * 3.
>>>> > 208733 is divisible by 7: 208733 = 29819 * 7.
>>>> > 29819 is not divisible by anything.
>>>> >
>>>> > So 29819 by 42 (7*3*2)
>>>> >
>>>> > would be a route.
>>>>
>>>> Aha. Missed the "30" bit.
>>>>
>>>> So, having found the factors, you would need to process them to find
>>>> the largest combination under 30.
>>>>
>>>> 2*3
>>>> 2*3*7
>>>> 2*7
>>>> 3*7
>>>>
>>>> are the possibilities (ignoring any number over 30).
>>>>
>>>> Of which 3*7 is the largest.
>>>>
>>>> So, 1,252,398 divided by 21 = 59,638
>>>>
>>>>
>>>> Is that the sort of thing you are looking for?
>>>>
>>>>
>>>
>>> Yes, that looks exactly what like what I'm looking for.  I'm going to try
>>> and wake up the algebra side of my brain that hasn't been used in years
>>> and
>>> see if I can digest all this.
>>>
>>> For the 2, 3, and 7, that is based solely on the last number being
>>> divisible
>>> by a prime number?
>>>
>>> Joao, Jason, thanks for the code.
>>>
>>> --
>>> -Dan Joseph
>>>
>>> www.canishosting.com - Unlimited Hosting Plans start @ $3.95/month.  Promo
>>> Code "NEWTHINGS" for 10% off initial order
>>>
>>> http://www.facebook.com/canishosting
>>> http://www.facebook.com/originalpoetry
>>>
>>
>> This seems to be working ...
>>
>> <?php
>> function findBestFactors($Value, $GroupSize, array &$Factors = null)
>>       {
>>       $Factors = array();
>>       foreach(range(1, ceil(sqrt($Value))) as $Factor)
>>               {
>>               if (0 == ($Value % $Factor))
>>                       {
>>                       if ($Factor <= $GroupSize)
>>                               {
>>                               $Factors[] = $Factor;
>>                               }
>>                       if ($Factor != ($OtherFactor = ($Value / $Factor)) && $OtherFactor
>> <= $GroupSize)
>>                               {
>>                               $Factors[] = $OtherFactor;
>>                               }
>>                       }
>>
>>               if ($Factor >= $GroupSize)
>>                       {
>>                       break;
>>                       }
>>               }
>>
>>       rsort($Factors);
>>
>>       return reset($Factors);
>>       }
>>
>> echo findBestFactors($argv[1], $argv[2], $Factors), PHP_EOL;
>> ?>
>>
>>
>> factors 1252398988 5000
>>
>> outputs  ...
>>
>> 4882
>>
>> and 21 for your value 1252398
>>
>> --
>> -----
>> Richard Quadling
>> "Standing on the shoulders of some very clever giants!"
>> EE : http://www.experts-exchange.com/M_248814.html
>> EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
>> Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
>> ZOPA : http://uk.zopa.com/member/RQuadling
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>

Thank you. It was a quick knock up, so could probably be optimized a
little more.

It will also not work beyond PHP_MAX_INT, unless the code is converted
to use the BCMath or GMP extension.

--
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling
From: Dotan Cohen on
On 22 April 2010 17:07, Dan Joseph <dmjoseph(a)gmail.com> wrote:
> Howdy,
>
> This is a math question, but I'm doing the code in PHP, and have expunged
> all resources... hoping someone can guide me here.  For some reason, I can't
> figure this out.
>
> I want to take a group of items, and divide them into equal groups based on
> a max per group.  Example.
>
> 1,252,398 -- divide into equal groups with only 30 items per group max.
>
> Can anyone guide me towards an algorithm or formula name to solve this?  PHP
> code or Math stuff is fine.  Either way...
>
> Thanks...
>

What is wrong with 626,299 groups of 2 items each (done in my head, so
I might be off a little)?

--
Dotan Cohen

http://bido.com
http://what-is-what.com
From: Richard Quadling on
On 23 April 2010 13:33, Dotan Cohen <dotancohen(a)gmail.com> wrote:
> What is wrong with 626,299 groups of 2 items each (done in my head, so
> I might be off a little)?

2, 3, 6, 7, 14 and 21 are all valid.


--
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling
From: tedd on
At 10:17 AM -0400 4/22/10, Dan Joseph wrote:
>On Thu, Apr 22, 2010 at 10:12 AM, Stephen <stephen-d(a)rogers.com> wrote:
>
>> 1,252,398 DIV 30 = 41,746 groups of 30.
>>
>> 1,252,398 MOD 30 = 18 items in last group
>>
>Well, the only problem with going that route, is the one group is not
>equally sized to the others. 18 is ok for a group in this instance, but if
>it was a remainder of only 1 or 2, there would be an issue. Which is where
>I come to looking for a the right method to break it equally.
>
>--
>-Dan Joseph


_Dan:

As I see it -- you are asking is "What would be the 'optimum' group
size for 1,252,398?"

"Optimum" here meaning:

1. A group size of 30 or under;
2. All groups being of equal size.

Is that correct?

There may not be an exact solution, but a first order attempt would
be to divide the total number by 30 and check the remainder (i.e.,
MOD), the do the same for 29, 28, 27... and so on.

The group size "solution" would be a number with a zero remainder OR
with a remainder closest to your group size.

That would be my first blush solution.

Cheers,

tedd

--
-------
http://sperling.com http://ancientstones.com http://earthstones.com