Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [php] NWD w php, nwd
Mati117
post
Post #1





Grupa: Zarejestrowani
Postów: 5
Pomógł: 0
Dołączył: 26.04.2007

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


MAm problem. Nie wiem jak napisać program obliczający największy wspólny dzielnik z 2 liczb. Potrzebuje to do mojego kalkulatora. Wiem coś że za pomoca mod, ale nie bardzo wiem jak.
Go to the top of the page
+Quote Post
strife
post
Post #2





Grupa: Przyjaciele php.pl
Postów: 2 605
Pomógł: 96
Dołączył: 22.10.2004
Skąd: UK

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


http://www.phpfreaks.com/quickcode/greates...divisor/445.php

Wystarczyło poszukać (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)

Jeśli chcesz coś swojego napisać to napewno przyda Ci się http://pl.wikipedia.org/wiki/Algorytm_Euklidesa

Cytat
napisać program


Jeśli już to skrypt, w php nie pisze się programów (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)

Pozdrawiam.
Go to the top of the page
+Quote Post
Mati117
post
Post #3





Grupa: Zarejestrowani
Postów: 5
Pomógł: 0
Dołączył: 26.04.2007

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


no ale ja bym chciał napisać za pomoca fonction


$a=$_POST['a'];
$b=$_POST['b'];
fonction NWd($a,$b)

i wiem że coś tutaj $amod$b
Go to the top of the page
+Quote Post
strife
post
Post #4





Grupa: Przyjaciele php.pl
Postów: 2 605
Pomógł: 96
Dołączył: 22.10.2004
Skąd: UK

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


Cytat(Mati117 @ 28.05.2007, 20:51:54 ) *
no ale ja bym chciał napisać za pomoca fonction
$a=$_POST['a'];
$b=$_POST['b'];
fonction NWd($a,$b)

i wiem że coś tutaj $amod$b


Co to jest fonction ? (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif) A powiedz mi link który ode mnie dostałeś nie używa funkcji ? Sprawdziłeś ten kod w ogóle ?

  1. <?php
  2.  
  3. function gcd($int_one, $int_two) {
  4. if ($int_one == 0 || $int_two == 0)
  5. return 1;
  6. // assign max and min
  7. $max = max($int_one, $int_two);
  8. $min = min($int_one, $int_two);
  9. // euclidean algorithm arrays
  10. $m = $n = $q = $r = array();
  11. // define our starting dividend and divisor
  12. $m[0] = $max;
  13. $n[0] = $min;
  14. // start our euclidean algorithm
  15. for ($i = 0; $r[($i - 1)] !== 0; $i++) {
  16. $q[$i] = floor(bcdiv($m[$i], $n[$i], 15));
  17. $r[$i] = $m[$i] - bcmul($q[$i], $n[$i]);
  18. $m[($i + 1)] = $n[$i];
  19. $n[($i + 1)] = $r[$i];
  20. }
  21. // reached a zero remainder, meaning divisor in last step is GCD
  22. return $n[($i - 1)];
  23. }
  24. $a = $_POST['a'];
  25. $b = $_POST['b'];
  26. echo gcd( $a, $b );
  27. ?>


Pff (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif)

~edit
Aż Ci temat na Przedszkole przeniosę (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif)
Go to the top of the page
+Quote Post
Mati117
post
Post #5





Grupa: Zarejestrowani
Postów: 5
Pomógł: 0
Dołączył: 26.04.2007

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


dzieki
Go to the top of the page
+Quote Post

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: 23.08.2025 - 04:02