Task:

Your task is to write a function which returns the sum of following series upto nth term(parameter).

Series: 1 + 1/4 + 1/7 + 1/10 + 1/13 + 1/16 +...

Rules:

  • You need to round the answer to 2 decimal places and return it as String.

  • If the given value is 0 then it should return 0.00

  • You will only be given Natural Numbers as arguments.

Examples:

SeriesSum(1) => 1 = "1.00"
SeriesSum(2) => 1 + 1/4 = "1.25"
SeriesSum(5) => 1 + 1/4 + 1/7 + 1/10 + 1/13 = "1.57"
my answer:
def series_sum(n):
# Happy Coding ^_^
temp_sum=1
if n==0:
temp_sum=0
while n>1:
temp_sum+=1/(3*n-2)
n-=1
return '%.2f'%temp_sum

优秀代码:

def series_sum(n):
return '{:.2f}'.format(sum(1.0/(3 * i + 1) for i in range(n)))

这个网站还是很不错的,从级数开始练习,每个阶段安装能力来匹配,我觉得这个模式比LeetCode好很多,因为LeetCode一上手感觉还是很难的,我这种菜鸡还是要先写一写基础的题目

反思:1.pyhton里面不可以用n--这种操作

    2.python的round函数有问题,对整数部分是奇数还是偶数有要求,不一定会进位,不如使用

         '%.2f'%temp_sum,确保变成浮点型     
												

codewars-7kyu:Sum of the first nth term of Series的更多相关文章

  1. LightOj 1096 - nth Term (矩阵快速幂,简单)

    题目 这道题是很简单的矩阵快速幂,可惜,在队内比赛时我不知什么时候抽风把模版中二分时判断的 ==1改成了==0 ,明明觉得自己想得没错,却一直过不了案例,唉,苦逼的比赛状态真让人抓狂!!! #incl ...

  2. LightOJ 1096 - nth Term 矩阵快速幂

    http://www.lightoj.com/volume_showproblem.php?problem=1096 题意:\(f(n)  = a * f(n-1) + b * f(n-3) + c, ...

  3. Sum of AP series——AP系列之和

    A series with same common difference is known as arithmetic series. The first term of series is 'a' ...

  4. 10个经典的C语言面试基础算法及代码

    10个经典的C语言面试基础算法及代码作者:码农网 – 小峰 原文地址:http://www.codeceo.com/article/10-c-interview-algorithm.html 算法是一 ...

  5. Project Euler 101 :Optimum polynomial 最优多项式

    Optimum polynomial If we are presented with the first k terms of a sequence it is impossible to say ...

  6. (Problem 42)Coded triangle numbers

    The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangl ...

  7. lightoj刷题日记

    提高自己的实力, 也为了证明, 开始板刷lightoj,每天题量>=1: 题目的类型会在这边说明,具体见分页博客: SUM=54; 1000 Greetings from LightOJ [简单 ...

  8. Project Euler:Problem 42 Coded triangle numbers

    The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangl ...

  9. 决战Leetcode: easy part(1-50)

    本博客是个人原创的针对leetcode上的problem的解法,所有solution都基本通过了leetcode的官方Judging,个别未通过的例外情况会在相应部分作特别说明. 欢迎互相交流! em ...

随机推荐

  1. SQL事务在存储过程的应用

    -- ============================================= -- Author: jf_ou -- Create date: 2017-08-22 -- Desc ...

  2. git使用总结(常用命令)

    前言 写这篇文章的目的是让新手能够操作git管理自己的代码,可能你知道git的各种命令但是对其使用顺序并不熟,比如我.所以有必要整合一篇关于命令使用步骤的文章,图片用的人家的,也没询问让不让用,可能会 ...

  3. JavaScript try-catch语句(错误处理)

    错误处理在处理程序设计中的重要性是毋庸置疑的,任何有影响力的web应用程序都需要一套完善的错误处理机制.当然,大多数佼佼者确实做到了这一点,但通常只有服务器端应用程序才能做到如此.实际上,服务器端团队 ...

  4. 前端 css+js实现返回顶部功能

    描述: 本文主要是讲,通过css+js实现网页中的[返回顶部]功能. 实现代码: HTML: <div> <button onclick="returnTop()" ...

  5. redis 集群目标、集群查看、配置方法及过程、哨兵配置启动

    集群目标 主从复制,读写分离:故障切换(通过哨兵实现) 查看集群状态 info replication 配置方法 只设置从数据库就可以了:最佳实践,在主数据库配置masterauth <mast ...

  6. Debian出现in the drive ‘/media/cdrom/’ and press enter解决办法

    没有光盘源解决打开/etc/apt/sources.list文件,注释掉cdrom那一行,然后再执行apt-get update更新下deb仓库这样以后再使用apt-get安装时就不会再搜寻cdrom ...

  7. ExpressRoute 合作伙伴和对等位置

    本文中的表格提供有关 ExpressRoute 连接提供商.ExpressRoute 地理覆盖范围.通过 ExpressRoute 支持的 Azure 服务以及 ExpressRoute 系统集成商 ...

  8. [UI] Article intro effects

    Article intro effects http://freebiesbug.com/code-stuff/article-intro-effects/

  9. Redis常用指令

    1.使用指令存储数据 不同数据类型的使用 1.String > 在以上指令中我们使用set指令向redis存进了一个数据类型为string,名为str1,值为123456.(如果你要问为什么,那 ...

  10. C++用法总结

    1.C++的绝对值符号 如果是整形的,就是abs() 如果是浮点型的,是fabs() 这两个函数都从属于库函数math.h #include <cmath> or #include< ...