beginning calculation algorithm