Skip to content

Commit

Permalink
Dodanie algorytmów: pierwiastek metodą Herona i całka nieoznaczona Ri…
Browse files Browse the repository at this point in the history
…emanna.
  • Loading branch information
xinulsw committed Feb 2, 2017
1 parent 51e9245 commit c1aa897
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
28 changes: 28 additions & 0 deletions docs/python/algorytmy/heron.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Obliczanie wartości pierwiastka kwadratowego z podanej liczby
# przy użyciu metody Herona: a(n) = (a(n-1) + x / a(n-1)) / 2
# <eCG>


def pierwiastek(x, d):
a = x # inicjalizacja szkuanej wartości
while abs(a - (x / a)) > d:
a = (a + (x / a)) / 2
return a


def main(args):
# liczba dla której szukamy pierwiastka
x = float(raw_input("Podaj liczbę: "))
# dokładność obliczeń
d = float(raw_input("Podaj dokładność: "))
# drukujemy wynik z dokładnością do 6 miejsc
print "Pierwiastek kwadratowy: {:.6f}".format(pierwiastek(x, d))
return 0


if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))
31 changes: 29 additions & 2 deletions docs/python/algorytmy/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ Przez wybór
:linenos:


Konwersja liczby dziesiętnej
============================
Konwersja liczby
================

.. raw:: html

Expand All @@ -86,3 +86,30 @@ Wersja ze stałym kluczem.

.. literalinclude:: szyfr_cezara.py
:linenos:


Pierwiastek kwadratowy
======================

Użycie metody Herona.

.. raw:: html

<div class="code_no">Kod nr <script>var code_no = code_no || 1; document.write(code_no++);</script></div>

.. literalinclude:: heron.py
:linenos:


Pole obszaru
============

Całka nieoznaczona Riemanna.
[Sprawdzić poprawność obliczeń.]

.. raw:: html

<div class="code_no">Kod nr <script>var code_no = code_no || 1; document.write(code_no++);</script></div>

.. literalinclude:: pole.py
:linenos:
33 changes: 33 additions & 0 deletions docs/python/algorytmy/pole.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Obliczanie pola obszaru ograniczoneg wykresem funkcji:
# y = x^2, prostymi x = a, x = b i osią OX
# (oznaczona całka Riemanna)
# <eCG>


def funkcja(x):
return x * x


def pole(a, b, n):
p = 0 # wyliczane pole
d = (b - a) / n # długość przedziałów w zakresie <a;b>
for i in range(n):
x = a + (d * i) + (d / 2) # punkty pośrednie przedziałów
p += abs(funkcja(x)) # suma pól prostokątów
return p


def main(args):
a = int(raw_input("Podaj lewy kraniec: "))
b = int(raw_input("Podaj prawy kraniec: "))
n = int(raw_input("Podaj liczbę przedziałów: "))
print "Pole: {:.3f}".format(pole(a, b, n))
return 0


if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))

0 comments on commit c1aa897

Please sign in to comment.