我最近加入了Python,我无法想出任何要做的事情。因此,我决定将所有Project Euler的东西移植到Python上,只是为了做一些事情。但基本上,下面的代码应该找到a,b和c的乘积,其中a + b + c = 1000.(Pythagorean triplet)
from math import sqrt
def solve():
product = 0
for a in range(1, 500):
for b in range(1, 500):
needed = sqrt(a*a + b*b)
if a + b + needed == 1000:
product = a * b * needed
return product
return product
print(solve())
此代码生成正确的结果,即31875000.0。我希望它返回一个整数,而不是浮点值。我试着做了
needed = round(sqrt(a*a + b*b))
但由于某种原因,这会返回数字498002,它甚至不接近相同的数字。我也尝试过使用数学库中的Floor,但这也是同样的事情,以及int()。有什么东西我可以忽略吗?或者这是一个错误?或者是什么。如果重要,我会使用Python 3.5。
答案 0 :(得分:2)
因为当您round()
关闭,或甚至int()
将sqrt()
结果转换为整数时,您正在失去精确度,例如i
为2且j
499
为i
,因为a^2 + b^2
非常小,499.0040079999358
的sqrt就像 - 499
- 四舍五入会给499 + 499 + 2
1000
}。而且你的程序错误地认为这是一个三元组,因为sqrt
是integer
。
您应该在返回之前将产品转换为from math import sqrt
def solve():
product = 0
for a in range(1, 500):
for b in range(1, 500):
needed = sqrt(a*a + b*b)
if a + b + needed == 1000:
product = a * b * needed
return int(product)
return product
print(solve())
,而不是舍入<section id="portfolio" ng-controller="portfolioController">
<div class="container-fluid">
<div class="row portfolio-row">
<div class="portfolio-left col-xs-12 col-md-6">
</div>
<div class="portfolio-right col-xs-12 col-md-6">
<div class="portfolio-content">
<div class="portfolio-title">
<h1><strong>DONE SOME STUFF.</strong></h1>
</div>
<div class="portfolio-filters-container">
<button class="portfolio-filters" ng-repeat="language in portfolio.languages" ng-click="portfolio.projectSort(language)">{{ language.name }}</button>
</div>
<div class="portfolio-carousel">
<carousel interval="portfolio.carouselInterval" no-wrap="noWrapSlides">
<slide ng-repeat="project in portfolio.projectsToShow">
<div class="portfolio-projects">
<img ng-src="{{ project.image }}" />
</div>
</slide>
</carousel>
</div>
<div class="portfolio-button">
<button class="label label-success">CONTACT ME =></button>
</div>
</div>
</div>
</div>
</div>
</section>
的结果。示例 -
#portfolio {
height: 100%;
width: 100%;
color: gray;
background-color: #101010;
border-bottom: 3px solid black;
}
#portfolio .container-fluid, #portfolio .row {
height: 100%;
}
.portfolio-left {
height: 100%;
background-image: url('../../images/jay-photograph.jpg');
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
border-right: 3px solid #101010;
}
.portfolio-right {
text-align: center;
padding-top: 16.5vh;
}
.portfolio-content {
width: 50%;
margin-left: auto;
margin-right: auto;
}
.portfolio-title h1 {
font-size: 3.1vw;
margin-bottom: 1.3vh;
}
.portfolio-filters-container {
text-align: center;
padding-bottom: 10px;
}
.portfolio-filters {
width: auto;
color: gray;
border: 1px solid gray;
border-radius: 5px;
background-color: black;
margin: 5px;
font-size: 0.7vw;
}
.portfolio-projects {
border-style: solid;
border-color: gray;
height: 100%;
width: 100%;
}
.portfolio-projects img {
max-height: 100%;
max-width: 100%;
background-color: rgba(82, 82, 82, 0.6);
}
.portfolio-button {
padding-top: 1.3vh;
}
.portfolio-button button {
font-size: 0.7vw;
border: 1px solid gray;
background-color: #101010;
color: gray;
}
@media only screen and (max-width: 992px) {
.portfolio-left {
height: 50%;
border-bottom: 3px solid black;
}
}