# Translate sum and long variables to Julia (VS Code)

**URL:** <https://discourse.julialang.org/t/translate-sum-and-long-variables-to-julia-vs-code/88145>\
**Category:** General Usage\
**Created:** [October 3, 2022, 6:53am UTC](https://discourse.julialang.org/t/translate-sum-and-long-variables-to-julia-vs-code/88145 "2022-10-03T06:53:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tryjulia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tryjulia/32/42826_2.png) [@tryjulia](https://discourse.julialang.org/u/tryjulia)\
**Post date:** [October 3, 2022, 6:53am UTC](https://discourse.julialang.org/t/translate-sum-and-long-variables-to-julia-vs-code/88145/1 "2022-10-03T06:53:34Z")

</div>

Hi, I want to write code to transform the mathematical expression of:  
y=sum from k=0 to k=infinite of x^k/k!  
where x and y are long real var.  
I try to write the code such this:

> function taylor(k)  
> k = (0:Inf)  
> x = long(x)  
> y = long(y)  
> if n == 0  
> end  
> if n\>0  
> for i in 1:n  
> f = k\*i  
> y= x^k/f  
> end  
> @show x  
> @show y  
> end  
> end  
> (k)=taylor(3)

or:

> function taylor(x)  
> u=0  
> v=1  
> k=0  
> a=1  
> while ((v-u)/u)\>=(1/10^10)  
> u=u+((x^k)/factorial(big(k)))  
> k=k+1  
> v=v+((x^a)/factorial(big(a)))  
> a=a+1  
> end  
> return u  
> end  
> (u,v)=taylor(6)  
> @show u,v

both didn’t work. I am new to Julia and have no experience in coding before. How to represent series of numbers from 1 to infinite to the x function, let say x= int64. And also how to write proper function e.g function taylor(k). What variable should I put inside taylor( ).

Thank you!!!

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 3, 2022, 7:28am UTC](https://discourse.julialang.org/t/translate-sum-and-long-variables-to-julia-vs-code/88145/2 "2022-10-03T07:28:42Z")

</div>

Calculating the factorial and the power for each iteration is going to be very slow. `BigFloat`s are also slow, but you don’t need them.

For each iteration you add a new term to your sum. Notice that

x\_k = x^k/k! \hspace{1cm} x\_{k+1} = x^{k+1}/(k+1)! 

So x\_{k+1} = x\_k\,\cdot\, x/(k+1)

Use this in your algorithm:

```julia
function mysum(x, K)
    xₖ = one(x) / 1 # zeroth term x^0/0! equals one
    s = xₖ # initialize sum
    for k in 1:K
        xₖ *= x / k # update term as shown above
        s += xₖ # update sum
    end
    return s
end

```

You should probably check for convergence, that is, occasionally check whether `s + xₖ == s`, at that point you can stop summing. I’ll leave adding that as an exercise for the reader 😁
