Showing posts with label math. Show all posts
Showing posts with label math. Show all posts

Thursday, May 5, 2016

Python solve Algorithm

# author:           Marcin Cherek
# python version:   3.4
# Date:             5 of Mai 2016
# language:         English
# Title:            solve Algorithm python implementation
import decimal
import logging

logging.basicConfig()
logger= logging.getLogger(__name__)
logger.setLevel(logging.INFO)

#Prepares the the calculation
#this method splits the Operators
#from the Numbers
def _prepare(calculation):
        for i in ["+","-","*","/"]:
                calculation=calculation.replace(i,";"+i+";")
        return calculation.split(";")

#Replaces the sub calculation
#with the Result of it
def _replaceRes(deb,result,zähler):
        deb[zähler-1]=""
        deb[zähler]=""
        deb[zähler+1]=result
        return deb

#Normal calculation of two
#Numebers and one Operator
def _calc(z1,z2,op):
        if(op=="+"):return z1+z2
        elif(op=="-"):return z1-z2
        elif(op=="*"):return z1*z2
        elif(op=="/"):return z1/z2
        
#Shrinks the Array
#It removes the empty elements
def _popArr(deb):
        p=0
        while p<len(deb):
            if deb[p]=="":
                deb.pop(p)
                continue
            p+=1
        return deb

#Most important method
#It iterates the calculation
#and corrdinates the subcalcs with the
#results of it
def _solve_simple(deb,op):
        result=0;zähler=0
        for i in deb:
            if i==op:
                logger.info(deb)
                result=_calc(decimal.Decimal(deb[zähler-1]),decimal.Decimal(deb[zähler+1]),op)
                _replaceRes(deb,result,zähler)
                result=0
            zähler+=1
        deb = _popArr(deb)
        return deb

#Calls the simple Alg
#First of course */ and after that +-
def solve(calculation):
        calculation = _prepare(calculation)
        for i in ["*","/","+","-"]:
                calculation=_solve_simple(calculation,i)
        return decimal.Decimal(calculation[len(calculation)-1])

Thursday, November 13, 2014

Factorial in Python

Factorial in Python

def Factorial(zahl):
    result=1
    for i in range(zahl):
        result=result*(zahl-i)
    return result


Author:Marcin
Language:Python 3.4
Info: The math function factorial ! in python. The only limitation you have is your hardware ,)

Power To math in Python

Power to in Python

def power(number,to):
    'potenz'
    result=number
    for i in range(int(to)-1):
        result=result*number
    return result


Author:Marcin
Language:Python 3.4
Info: The implementation of the power to math function in python. Its not limited, only by your ramspace ,)

Square root in python

Square root in python

from math import *

def square_root(number,wie_genau):
    'Heron'
    if number>0:
        x_one=decimal.Decimal(number);y_one=decimal.Decimal(1)
        for i in range(int(wie_genau)):
            x_two=decimal.Decimal(((x_one+y_one)/2))
            y_two=decimal.Decimal((number/x_one))
            #
            x_one=x_two
            y_one=y_two
        return decimal.Decimal(x_one)
    else:
        print("No Negative Numbers!")


Author:Marcin Cherek
Language:Python 3.4
Info: A little Implementation of the math function square root in python. I am using the Heron Algorithm.

Sunday, November 2, 2014

Fibonacci in python

Fibonacci Python


import os
import sys

def fib(a,b,count):
    c=a+b
    a=c
    b=c+b
    print(a)
    print(b)
    if count<100: a="" b="" count="" else:="" fib="" nde="" pre="" print="">


Author:Marcin
Language:Python 3.4
Infos: 
fibonacci Numbers with recursiv programming in python. Shows the next 100 numbers of fib function. But you can show more. Just manipulate the count in the fib function.
Video: https://www.youtube.com/watch?v=MrhpbPXxZbY