当前位置:网站首页>2021-05-04: given a non negative integer C, you need to judge whether there are two integers a and B, so that a*a+b*b=c.
2021-05-04: given a non negative integer C, you need to judge whether there are two integers a and B, so that a*a+b*b=c.
2022-06-24 15:52:00 【Fuda scaffold constructor's daily question】
2021-05-04: Given a nonnegative integer c, You have to decide if there are two integers a and b, bring a_a+b_b=c.【 give an example 】c=5 when , return true.c=4 when , return true.c=3 when , return false.
Fuda answer 2021-05-04:
The sum of four squares Theorem . Time complexity :O(sqrt(N)). Spatial complexity :O(1).
1.n Always divide by 4, Until it's not divisible .
2.n%8, If yu 7, Go straight back to 4.
3. from 1 To sqrt(n) Start the cycle ,a_a+b_b=c When established ,a and b Not for 0, return 2;a and b There is one for 0, return 1.
4. return 3.
5. In the subject , The return value is 1 and 2 Yes. true. The return value is 3 and 4 Yes. false.
The code to use golang To write . The code is as follows :
package main
import (
"fmt"
"math"
)
func main() {
for i := 1000; i <= 1100; i++ {
ret := isSquares(i)
fmt.Println(i, ret)
}
}
//4+1+1+1
func isSquares(n int) bool {
return numSquares(n) <= 2
}
func numSquares(n int) int {
for n&3 == 0 { //n%4==0
n >>= 2 //n/=4
}
if n&7 == 7 { //n%8==7
return 4
}
a := 0
for a*a <= n {
b := int(math.Sqrt(float64(n - a*a)))
if a > b {
break
}
if a*a+b*b == n {
ret := 0
if a != 0 {
ret++
}
if b != 0 {
ret++
}
return ret
}
a += 1
}
return 3
}The results are as follows :
边栏推荐
- Mongodb introductory practical tutorial: learning summary directory
- Solution of intelligent all in one machine in expressway service area
- 刚刚阿里面软件测试回来,3+1面任职阿里P7,年薪28*15薪
- Cloud + community [play with Tencent cloud] essay solicitation activity winners announced
- 一文理解OpenStack网络
- 如何轻松实现在线K歌房,与王心凌合唱《山海》
- [my advanced OpenGL learning journey] learning notes of OpenGL coordinate system
- New de debugging
- Database tools in intelij can connect but cannot display schema, tables
- Paper: Google TPU
猜你喜欢

为什么企业实施WMS仓储管理系统很容易失败

The equipment is connected to the easycvr platform through the national standard gb28181. How to solve the problem of disconnection?

设备通过国标GB28181接入EasyCVR平台,出现断流情况该如何解决?

Linux记录-4.22 MySQL5.37安装(补充)

Nifi from introduction to practice (nanny level tutorial) - environment

我与“Apifox”的网络情缘

国产最长寿的热销手机,苹果也不是对手,总算让国产手机找回面子

日志记录真没你想的那么简单

Mongodb introductory practical tutorial: learning summary directory

Logging is not as simple as you think
随机推荐
"Industry outlook" analysis of five major trends in China's security video surveillance industry
还在担心漏测吗?快来使用jacoco统计下代码覆盖率
clang: warning: argument unused during compilation: ‘-no-pie‘ [-Wunused-command-line-argument]
How to optimize performance
The equipment is connected to the easycvr platform through the national standard gb28181. How to solve the problem of disconnection?
Is it safe for futures companies to open accounts
Task priority motion planning of floating base
一文详解JackSon配置信息
How to efficiently transfer enterprise business data?
【Prometheus】4. Monitoring cases
【Prometheus】6. Prometheus and kubernetes (incomplete)
Instruction document for online written examination assistance of smart side school recruitment
10 hands-free idea plug-ins. These codes do not need to be written (the second bullet)
期货公司开户安全吗
Very exciting! 12000 words summarized the theory of network technology, reviewing the old and learning the new
实现领域驱动设计 - 使用ABP框架 - 领域逻辑 & 应用逻辑
【我的OpenGL学习进阶之旅】OpenGL的坐标系的学习笔记
CAP:多重注意力机制,有趣的细粒度分类方案 | AAAI 2021
One article explains Jackson configuration information in detail
Linux record -4.22 MySQL 5.37 installation (supplementary)