Wrote the recurrence relation in scala