Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Small and medium teams
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
cryptography-research-lab
go-factorization
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
go-factorization
/
factorization.go
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
49 lines (39 loc) · 1.38 KB
main
Breadcrumbs
go-factorization
/
factorization.go
Copy path
Top
File metadata and controls
Code
Blame
49 lines (39 loc) · 1.38 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package factorization
import "math"
// Factorization 对正整数进行因式分解
func Factorization(n uint64) []uint64 {
factorSlice := make([]uint64, 0)
// 平方根之后就不可能了因此就没必要继续了
factorEnd := uint64(math.Ceil(math.Sqrt(float64(n))))
for n > 0 {
// 启用素数表加速,跳着往后寻找约数,性能有明显上升
// 仅当n小于这个数字时才启用素数表来加速,否则就还是常规的递增迭代,这是因为素数表的数据规模是有限的,要能够兼容数值超出素数表边界的情况
factor, isFindSuccess, isCanFind := DefaultPrimeNumberTable.FindFirstFactor(n)
if !isCanFind {
// 普通的递增迭代判断,会有一些无效的判断,略有浪费
// 采取效率慢点的方式,O(n^2)
factor, isFindSuccess = findFirstFactorByEnd(factorEnd, n)
}
if isFindSuccess {
factorSlice = append(factorSlice, factor)
n /= factor
} else {
// 到达了边界值仍未寻找到,n已经没办法再约掉了
break
}
}
// 如果有未分解完的,则把它也追加到结果中
if n > 1 {
factorSlice = append(factorSlice, n)
}
return factorSlice
}
// 暴力搜索第一个因子
func findFirstFactorByEnd(factorEnd, n uint64) (firstFactor uint64, isFindSuccess bool) {
for i := uint64(2); i <= factorEnd; i++ {
if n%i == 0 {
return i, true
}
}
return 0, false
}
You can’t perform that action at this time.