PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 300,363 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,457 people online right now. Registration is fast and FREE... Join Now!




GMP and Divided by

 

GMP and Divided by

ellisgl

2 Jul, 2009 - 05:22 AM
Post #1

D.I.C Head
Group Icon

Joined: 10 Nov, 2007
Posts: 118



Thanked: 3 times
Dream Kudos: 25
My Contributions
I can get this to work, if I were doing normal ints. Since i'm using a number passed the max, I need to use the GMP functions and now I'm having issues.Here's the code.

CODE

<?php
$a   = gmp_init("2431418762");
$ret = "";

while(gmp_cmp($a, "0") > 0)
{
    for($x = 64; $x > 1; --$x)
     {
        if(gmp_mod($a, $x) === 0)
         {
            $ret     = $x.' ,'.$ret;
            $a       = gmp_div($a, $x);
            echo gmp_strval($a).'<br />';
            break;
         }
     }
    if($x === 1)
     {
        break;
     }
}


User is offlineProfile CardPM
+Quote Post


CTphpnwb

RE: GMP And Divided By

2 Jul, 2009 - 05:55 AM
Post #2

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 2,065



Thanked: 151 times
Dream Kudos: 100
Expert In: PHP

My Contributions
Do you have gmp installed?
http://www.php.net/manual/en/gmp.requirements.php
User is offlineProfile CardPM
+Quote Post

ellisgl

RE: GMP And Divided By

2 Jul, 2009 - 05:57 AM
Post #3

D.I.C Head
Group Icon

Joined: 10 Nov, 2007
Posts: 118



Thanked: 3 times
Dream Kudos: 25
My Contributions
QUOTE(CTphpnwb @ 2 Jul, 2009 - 05:55 AM) *

I sure do. I get the first division of the number, then it stops.
User is offlineProfile CardPM
+Quote Post

CTphpnwb

RE: GMP And Divided By

2 Jul, 2009 - 06:31 AM
Post #4

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 2,065



Thanked: 151 times
Dream Kudos: 100
Expert In: PHP

My Contributions
Should there be more?
When I do it without gmp (I don't have it) I get the same thing:
CODE
<?php
$a = 2431418762;
$ret = "";

while($a> 0)
{
          for($x = 64; $x > 1; --$x)
              {
                    if(($a % $x) === 0)
                        {
                              $ret = $x.' ,'.$ret;
                              $a = $a/$x;
                              echo $a.'<br />';
                              break;
                        }
              }
          if($x === 1)
              {
                   echo "x=1";
                   break;
              }
    }
?>

results:
56544622.3721
28272311.186
x=1
User is offlineProfile CardPM
+Quote Post

ellisgl

RE: GMP And Divided By

2 Jul, 2009 - 07:04 AM
Post #5

D.I.C Head
Group Icon

Joined: 10 Nov, 2007
Posts: 118



Thanked: 3 times
Dream Kudos: 25
My Contributions
QUOTE(CTphpnwb @ 2 Jul, 2009 - 06:31 AM) *

results:
56544622.3721
28272311.186
x=1


There shouldn't be a remainder....?!
User is offlineProfile CardPM
+Quote Post

CTphpnwb

RE: GMP And Divided By

2 Jul, 2009 - 08:33 AM
Post #6

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 2,065



Thanked: 151 times
Dream Kudos: 100
Expert In: PHP

My Contributions
I'm not sure what you're trying to accomplish, but bc math functions seem to work:
http://www.php.net/manual/en/ref.bc.php
CODE
<?php
$a = "2431418762";
$ret = "";
echo $a;
while((int)$a > 0 )
{
    for($x = 64; $x > 1; --$x)
    {
        $a = $a."";
        $xx = $x."";
//        echo $a." --- ".$xx." div: ";
//        echo bcdiv($a,$xx)."<br>";
        if(bcdiv($a,$xx) === "")
        {
            echo $a." ".$xx." ";
            $ret = $xx.' ,'.$ret;
            $a = bcdiv($a,$xx);
            echo $a.'<br />';
            break;
        }
    }
          if($xx === "1")
              {
                   break;
              }
}
?>

User is offlineProfile CardPM
+Quote Post

ellisgl

RE: GMP And Divided By

2 Jul, 2009 - 09:12 AM
Post #7

D.I.C Head
Group Icon

Joined: 10 Nov, 2007
Posts: 118



Thanked: 3 times
Dream Kudos: 25
My Contributions
I'm working on a encoder/decoder for numbers. Basically coding them into characters.
Here's the code that works on 32 bit unsigned range:

CODE

<?php
// Set up en/decoder ring
// 65 (index 0 - 64) characters
$enring = array(0,   1,   2,   3,
                4,   5,   6,   7,
                8,   9,
                'a', 'b', 'c', 'd',
                'e', 'f', 'g', 'h',
                'i', 'j', 'k', 'l',
                'm', 'n', 'o', 'p',
                'q', 'r', 's', 't',
                'u', 'v', 'w', 'x',
                'y', 'z',
                'A', 'B', 'C', 'D',
                'E', 'F', 'G', 'H',
                'I', 'J', 'K', 'L',
                'M', 'N', 'O', 'P',
                'Q', 'R', 'S', 'T',
                'U', 'V', 'W', 'X',
                'Y', 'Z',
                '_', '-', '~');

$dering = array(0,         1,         2,         3,
                4,         5,         6,        7,
                8,         9,
                'a' => 10, 'b' => 11, 'c' => 12, 'd' => 13,
                'e' => 14, 'f' => 15, 'g' => 16, 'h' => 17,
                'i' => 18, 'j' => 19, 'k' => 20, 'l' => 21,
                'm' => 22, 'n' => 23, 'o' => 24, 'p' => 25,
                'q' => 26, 'r' => 27, 's' => 28, 't' => 29,
                'u' => 30, 'v' => 31, 'w' => 32, 'x' => 33,
                'y' => 34, 'z' => 35,
                'A' => 36, 'B' => 37, 'C' => 38, 'D' => 39,
                'E' => 40, 'F' => 41, 'G' => 42, 'H' => 43,
                'I' => 44, 'J' => 45, 'K' => 46, 'L' => 47,
                'M' => 48, 'N' => 49, 'O' => 50, 'P' => 51,
                'Q' => 52, 'R' => 53, 'S' => 54, 'T' => 55,
                'U' => 56, 'V' => 57, 'W' => 58, 'X' => 59,
                'Y' => 60, 'Z' => 61,
                '_' => 62, '-' => 63, '~' => 64);

//
function shortenTweetID($tweetID)
{
    global $enring;
    $ret = "";

    for($i = $tweetID; $tweetID > 0;)
     {
        for($x = 64; $x > 1; --$x)
         {
            if(($tweetID%$x) === 0)
             {
                $ret     = $enring[$x].$ret;
                $tweetID = ($tweetID/$x);
                break;
             }
         }
        if($x === 1)
         {
            break;
         }
     }
    return $ret;
}

function enlargeTweetID($tweetID)
{
    global $dering;
    $ret = 1;
    $arr = preg_split('//', $tweetID, -1, PREG_SPLIT_NO_EMPTY);

    foreach($arr as $char)
     {
        $ret = ($ret * $dering[$char]);
     }

    return $ret;  
}
$a = 1280;
$b = shortenTweetID($a);
$c = enlargeTweetID($b);

echo $a.' : '.$b.' : '.$c;


User is offlineProfile CardPM
+Quote Post

ellisgl

RE: GMP And Divided By

2 Jul, 2009 - 05:21 PM
Post #8

D.I.C Head
Group Icon

Joined: 10 Nov, 2007
Posts: 118



Thanked: 3 times
Dream Kudos: 25
My Contributions
Ack it's a prime number! That's the problem. Have to come up with a way to deal with a prime number.

User is offlineProfile CardPM
+Quote Post

CTphpnwb

RE: GMP And Divided By

2 Jul, 2009 - 06:43 PM
Post #9

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 2,065



Thanked: 151 times
Dream Kudos: 100
Expert In: PHP

My Contributions
If N is a prime number then N % x should only be 0 if x=1, so there's still a problem with gmp.


This post has been edited by CTphpnwb: 2 Jul, 2009 - 06:43 PM
User is offlineProfile CardPM
+Quote Post

ellisgl

RE: GMP And Divided By

2 Jul, 2009 - 06:44 PM
Post #10

D.I.C Head
Group Icon

Joined: 10 Nov, 2007
Posts: 118



Thanked: 3 times
Dream Kudos: 25
My Contributions
QUOTE(CTphpnwb @ 2 Jul, 2009 - 06:43 PM) *

If N is a prime number then N % x should only be 0 if x=1, so there's still a problem with gmp.


Well, the code stops at 2 thou. I think I have a solution that I'm still writing up. Will post results.
User is offlineProfile CardPM
+Quote Post

ellisgl

RE: GMP And Divided By

2 Jul, 2009 - 10:19 PM
Post #11

D.I.C Head
Group Icon

Joined: 10 Nov, 2007
Posts: 118



Thanked: 3 times
Dream Kudos: 25
My Contributions
Ok - I think I have it. Need to add more utf stuff to make it better:
CODE
<?php

function encodeTweetID($tweetID)
{
    $a   = $b  = gmp_init("{$tweetID}");
    $s   = 0;
    $ret = "";

    // 0 and 1 mean nothing since we are using division and multipliction.
    $enring  = array(0,           1,           2,          3,
                     4,           5,           6,          7,
                     8,           9,
                     'a' ,  'b' ,  'c',  'd',
                     'e' ,  'f' ,  'g',  'h',
                     'i' ,  'j' ,  'k',  'l',
                     'm' ,  'n' ,  'o',  'p',
                     'q' ,  'r' ,  's',  't',
                     'u' ,  'v' ,  'w',  'x',
                     'y' ,  'z' ,
                     'A' ,  'B' ,  'C',  'D',
                     'E' ,  'F' ,  'G',  'H',
                     'I' ,  'J' ,  'K',  'L',
                     'M' ,  'N' ,  'O',  'P',
                     'Q' ,  'R' ,  'S',  'T',
                     'U' ,  'V' ,  'W',  'X',
                     'Y' ,  'Z' ,
                     '~' ,  '!' ,  '@',  '$',
                     '^' ,  '&' ,  '*',  '(',
                     ')' ,  '_' ,  '-',  '=',
                     '[' ,  ']' ,  '{',  '}',
                     '|' ,  ':' ,  ';',  '"',
                     '\'',  '<' ,  '>',  '`',
                     ',' ,  '.' ,  '?',  '\\',
                     '€' ,  'ƒ' ,  '„',  '…',
                     '†' ,  '‡' ,  '‰',  'Š',
                     '‹' ,  'Œ' ,  'Ž', '‘',
                     '’' , '“' );

    $dering  = array(0,           1,           2,          3,
                     4,           5,           6,          7,
                     8,           9,
                     'a'  => 10,  'b'  => 11,  'c' => 12,  'd' => 13,
                     'e'  => 14,  'f'  => 15,  'g' => 16,  'h' => 17,
                     'i'  => 18,  'j'  => 19,  'k' => 20,  'l' => 21,
                     'm'  => 22,  'n'  => 23,  'o' => 24,  'p' => 25,
                     'q'  => 26,  'r'  => 27,  's' => 28,  't' => 29,
                     'u'  => 30,  'v'  => 31,  'w' => 32,  'x' => 33,
                     'y'  => 34,  'z'  => 35,
                     'A'  => 36,  'B'  => 37,  'C' => 38,  'D' => 39,
                     'E'  => 40,  'F'  => 41,  'G' => 42,  'H' => 43,
                     'I'  => 44,  'J'  => 45,  'K' => 46,  'L' => 47,
                     'M'  => 48,  'N'  => 49,  'O' => 50,  'P' => 51,
                     'Q'  => 52,  'R'  => 53,  'S' => 54,  'T' => 55,
                     'U'  => 56,  'V'  => 57,  'W' => 58,  'X' => 59,
                     'Y'  => 60,  'Z'  => 61,
                     '~'  => 62,  '!'  => 63,  '@' => 64,  '$' => 65,
                     '^'  => 66,  '&'  => 67,  '*' => 68,  '(' => 69,
                     ')'  => 70,  '_'  => 71,  '-' => 72,  '=' => 73,
                     '['  => 74,  ']'  => 75,  '{' => 76,  '}' => 77,
                     '|'  => 78,  ':'  => 79,  ';' => 80,  '"' => 81,
                     '\'' => 82,  '<'  => 83,  '>' => 84,  '`' => 85,
                     ','  => 86,  '.'  => 87,  '?' => 88,  '\\'=> 89,
                     '€'  => 90,  'ƒ'  => 91,  '„' => 92,  '…' => 93,
                     '†'  => 94,  '‡'  => 95,  '‰' => 96,  'Š' => 97,
                     '‹'  => 98,  'Œ'  => 99,  'Ž' => 100, '‘' => 101,
                     '’'  => 102, '“'  => 103);

    while(intval(gmp_cmp($a, "0")) > 0)
     {
        // $x must be odd.
        for($x = 103; $x > 1; --$x)
         {
            $c = 0;
            $xx = gmp_init($x);

            if(gmp_intval(gmp_mod($a, $xx)) === 0)
             {
                $ret     = $enring[$x].$ret;
                $a       = gmp_div($a, $xx);
                $c       = 1;
                break;
             }
         }

        if(intval(gmp_cmp($a, "1")) === 0)
         {
           $a   = gmp_sub($a, '1');
           break;
         }

        if($x <= 1 && intval(gmp_cmp($a, "0")) === 1 && $c === 0)
         {
          
            // Dealing with prime. so subtract one
            $ret = "";
            $b     = gmp_sub($b, '1');
            $a     = $b;
            ++$s;
         }
     }

    if($s > 2)
     {
        $ret = encodeTweetID($s).'+'.$ret;
     }
   return $ret;
}

echo encodeTweetID(2431418762).'<br />';
echo encodeTweetID(100);


This post has been edited by ellisgl: 2 Jul, 2009 - 10:20 PM
User is offlineProfile CardPM
+Quote Post

ellisgl

RE: GMP And Divided By

2 Jul, 2009 - 10:52 PM
Post #12

D.I.C Head
Group Icon

Joined: 10 Nov, 2007
Posts: 118



Thanked: 3 times
Dream Kudos: 25
My Contributions
Fixed code (Had an array off):
CODE
<?php
// 2,431,418,762 <-- id
// 5)+v\„…“      <-- Shortend

function encodeTweetID($tweetID)
{
    $a   = $b  = gmp_init("{$tweetID}");
    $s   = 0;
    $ret = "";

    // 0 and 1 mean nothing since we are using division and multipliction.
    $enring  = array(0,           1,           2,          3,
                     4,           5,           6,          7,
                     8,           9,
                     'a' ,  'b' ,  'c',  'd',
                     'e' ,  'f' ,  'g',  'h',
                     'i' ,  'j' ,  'k',  'l',
                     'm' ,  'n' ,  'o',  'p',
                     'q' ,  'r' ,  's',  't',
                     'u' ,  'v' ,  'w',  'x',
                     'y' ,  'z' ,
                     'A' ,  'B' ,  'C',  'D',
                     'E' ,  'F' ,  'G',  'H',
                     'I' ,  'J' ,  'K',  'L',
                     'M' ,  'N' ,  'O',  'P',
                     'Q' ,  'R' ,  'S',  'T',
                     'U' ,  'V' ,  'W',  'X',
                     'Y' ,  'Z' ,
                     '~' ,  '!' ,  '@',  '$',
                     '^' ,  '&' ,  '*',  '(',
                     ')' ,  '_' ,  '-',  '=',
                     '[' ,  ']' ,  '{',  '}',
                     '|' ,  ':' ,  ';',  '"',
                     '\'',  '<' ,  '>',  '`',
                     ',' ,  '.' ,  '?',  '\\',
                     '€' ,  'ƒ' ,  '„',  '…',
                     '†' ,  '‡' ,  '‰',  'Š',
                     '‹' ,  'Œ' ,  'Ž', '‘',
                     '’' , '“' );

    $dering  = array(0,           1,           2,          3,
                     4,           5,           6,          7,
                     8,           9,
                     'a'  => 10,  'b'  => 11,  'c' => 12,  'd' => 13,
                     'e'  => 14,  'f'  => 15,  'g' => 16,  'h' => 17,
                     'i'  => 18,  'j'  => 19,  'k' => 20,  'l' => 21,
                     'm'  => 22,  'n'  => 23,  'o' => 24,  'p' => 25,
                     'q'  => 26,  'r'  => 27,  's' => 28,  't' => 29,
                     'u'  => 30,  'v'  => 31,  'w' => 32,  'x' => 33,
                     'y'  => 34,  'z'  => 35,
                     'A'  => 36,  'B'  => 37,  'C' => 38,  'D' => 39,
                     'E'  => 40,  'F'  => 41,  'G' => 42,  'H' => 43,
                     'I'  => 44,  'J'  => 45,  'K' => 46,  'L' => 47,
                     'M'  => 48,  'N'  => 49,  'O' => 50,  'P' => 51,
                     'Q'  => 52,  'R'  => 53,  'S' => 54,  'T' => 55,
                     'U'  => 56,  'V'  => 57,  'W' => 58,  'X' => 59,
                     'Y'  => 60,  'Z'  => 61,
                     '~'  => 62,  '!'  => 63,  '@' => 64,  '$' => 65,
                     '^'  => 66,  '&'  => 67,  '*' => 68,  '(' => 69,
                     ')'  => 70,  '_'  => 71,  '-' => 72,  '=' => 73,
                     '['  => 74,  ']'  => 75,  '{' => 76,  '}' => 77,
                     '|'  => 78,  ':'  => 79,  ';' => 80,  '"' => 81,
                     '\'' => 82,  '<'  => 83,  '>' => 84,  '`' => 85,
                     ','  => 86,  '.'  => 87,  '?' => 88,  '\\'=> 89,
                     '€'  => 90,  'ƒ'  => 91,  '„' => 92,  '…' => 93,
                     '†'  => 94,  '‡'  => 95,  '‰' => 96,  'Š' => 97,
                     '‹'  => 98,  'Œ'  => 99,  'Ž' => 100, '‘' => 101,
                     '’'  => 102, '“'  => 103);

    while(intval(gmp_cmp($a, "0")) > 0)
     {
        // $x must be odd.
        for($x = 103; $x > 1; --$x)
         {
            $c = 0;
            $xx = gmp_init($x);

            if(gmp_intval(gmp_mod($a, $xx)) === 0)
             {
                $ret     = $enring[$x].$ret;
                $a       = gmp_div($a, $xx);
                $c       = 1;
                break;
             }
         }

        if(intval(gmp_cmp($a, "1")) === 0)
         {
           $a   = gmp_sub($a, '1');
           break;
         }

        if($x <= 1 && intval(gmp_cmp($a, "0")) === 1 && $c === 0)
         {
          
            // Dealing with prime. so subtract one
            $ret = "";
            $b     = gmp_sub($b, '1');
            $a     = $b;
            ++$s;
         }
     }

    if($s > 2)
     {
        $ret = encodeTweetID($s).'+'.$ret;
     }
   return $ret;
}

echo encodeTweetID(2431418762).'<br />';
echo encodeTweetID(100);

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 08:23PM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month