Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Hardcorowa matma i php, 8^10000
NuLL
post
Post #1





Grupa: Zarejestrowani
Postów: 2 262
Pomógł: 21
Dołączył: 3.05.2004
Skąd: Sopot, Krakow, W-wa

Ostrzeżenie: (0%)
-----


Hej,

Mam do rozwiazana dwa dziwne problemy w php i nie mam pojecia jak je ugrysc. W php jest kalkulator binarny a ja nie moge go uzysc.

1) 8 do potegi 10000 (IMG:http://forum.php.pl/style_emoticons/default/blink.gif)
2) Dodawanie swoch 1000 cyfrowych liczb (IMG:http://forum.php.pl/style_emoticons/default/aarambo.gif)

Bede wdzieczny za pomoc w dowolnym wypadku (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg) Nie mam zielonego pojecia jak ugrysc ten cokolwiek dziwny jak na php problem. Liczby w drugim sa wczytywanie jako stringi znak po znaku. Kombinowalem z tablicami ale jakos slabo to szlo.

Any ideas ?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
dr_bonzo
post
Post #2





Grupa: Przyjaciele php.pl
Postów: 5 724
Pomógł: 259
Dołączył: 13.04.2004
Skąd: N/A

Ostrzeżenie: (0%)
-----


I tak bylem szybszy (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif) (NuLL wie (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif) )

  1. <pre>
  2. <?php
  3.  
  4. class BigNumberAdder
  5. {
  6.     public function add( $num_X, $num_Y )
  7.     {
  8.         $length = $num_X->getLength();
  9.         $sum = BigNumber::createZero( $length + 1 ); // bo moze wystapic przeniesienie z ost pozycji: 9999 + 9999 --> 19998
  10.         
  11.         $carry = 0;
  12.         $partialSum = 0;
  13.         
  14.         // cyfry Xa i Yka
  15.         $x = 0;
  16.         $y = 0;
  17.         $s = 0; // cyfra sumy
  18.         // od ostatniej cyfry (najmniej znaczacej -- od jednosci)
  19.         for ( $i = $length - 1; $i >= 0; $i-- )
  20.         {
  21.             $x = $num_X->getDigit( $i );
  22.             $y = $num_Y->getDigit( $i );
  23.             $partialSum = $x + $y + $carry;
  24.             
  25.             print( "$x + $y + $carry =  $partialSum | C = $carry , S = $s<br />" );
  26.             
  27.             $carry = (int)( $partialSum / 10 );
  28.             $s = $partialSum % 10;
  29.             $sum->setdigit( $i + 1, $s );
  30.         }
  31.         
  32.         $sum->setDigit( 0, $carry );
  33.         return $sum;
  34.     }
  35. }
  36.  
  37. class BigNumber
  38. {
  39.     private $digits = array();
  40.     private $length = 0; 
  41.     
  42.     private function __construct()
  43.     {
  44.     }
  45.     
  46.     // tylko N pierwszych cyft jest akceptowanych
  47.     // '1245' --> array( 1,2,4,5)
  48.     static public function createFromString( $strNumber )
  49.     {
  50.         $number = new BigNumber();
  51.         
  52.         $number->length = strlen( $strNumber );
  53.         
  54.         for ( $i = 0; $i < $number->length; $i++ )
  55.         {
  56.             $number->digits[ $i ] = intval( $strNumber{$i} );
  57.         }
  58.         
  59.         return $number;
  60.     }
  61.     
  62.     static public function createZero( $length )
  63.     {
  64.         $number = new BigNumber();
  65.         $number->digits = array_fill( 0, $length, 0 );
  66.         $number->length = $length;
  67.         
  68.         return $number;
  69.     }
  70.     
  71.     public function __toString()
  72.     {
  73.         return implode( '', $this->digits );
  74.     }
  75.     
  76.     public function getLength()
  77.     {
  78.         return $this->length;
  79.     }
  80.     
  81.     public function getDigit( $i )
  82.     {
  83.         return $this->digits[ $i ];
  84.     }
  85.     
  86.     public function setDigit( $i, $digit )
  87.     {
  88.         $this->digits[ $i ] = $digit;
  89.     }
  90. }
  91.  
  92. $num_1 = BigNumber::createFromString( '1245' );
  93. $num_2 = BigNumber::createFromString( '9768' );
  94. //$num_2 = BigNumber::createZero( 4 );
  95.  
  96. $adder = new BigNumberAdder();
  97. $sum = $adder->add( $num_1, $num_2 );
  98.  
  99.  
  100. echo $num_1;
  101. echo " + ";
  102. echo $num_2;
  103. echo " = ";
  104. echo( $sum );
  105. ?></pre>


------------
edit 1:
1) 8 do potegi 10000 (IMG:http://forum.php.pl/style_emoticons/default/blink.gif)

8^10000 = (2^3)^10000 = 2^30000 czyli binarnie
100...000 // 30k zer, a zeby to zapisac dziesietnie to trzeba... podniesc 8 do 10000 potegi (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif)
Ew mozesz napisac multiplikator podobnie do addera, operujacy na stringu --> tablicy intow
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 15.10.2025 - 22:24