Martyr2项目实现——Number部分问题求解(3) Prime Factorization
Martyr2项目实现——Number部分问题求解(3) Prime Factorization
质因子分解
问题描述:
Prime Factorization – Have the user enter a number and find all Prime Factors (if there are any) and display them.
翻译:
质因子分解:给定一个整数N,找到并输出他的全部质因子
原理:
质因数分解,是将一个正整数写成几个约数的成绩,并且这些约数都是质数
给定一个合数n(这里,n是待分解的正整数),试除法看成是用小于等于\(\sqrt{n}\)的每个素数去试除待分解的整数。如果找到一个数能够整除除尽,这个数就是待分解整数的因子。试除法一定能够找到n的因子。因为它检查n的所有可能的因子,所以如果这个算法“失败”,也就证明了n是个素数(参考wikipedia:试除法)
算法实现:
public static ArrayList<Long> PrimeFactor(long N){ //使用短除法来找到一个合数的全部质因子
long k = (long)Math.sqrt(N);
ArrayList<Long> list = new ArrayList<>();
for(long i=2;i<=k;i++) {
while (N % i == 0) {
list.add(i);
N /= i;
}
}
if(N!=1) list.add(N);
return list; //返回质因子构成的列表
}
Martyr2项目实现——Number部分问题求解(3) Prime Factorization的更多相关文章
- Martyr2项目实现——Number部分的问题求解 (1) Find Pi to Nth Digit
Martyr2项目实现--Number部分的问题求解 (1) Find Pi to Nth Digit Find Pi to Nth Digit 问题描述: Find PI to the Nth Di ...
- 最小公倍数 SRM 661 Div1 250: MissingLCM
Problem Statement The least common multiple (denoted "lcm") of a non-empty sequence of pos ...
- about how to determine a prime number
(1) if divided by 2 or 3, then no; (2) we only have to go through prime factors; because a composite ...
- 4_Is Prime
4 // // ViewController.swift // Is Prime // // Created by ZC on 16/1/9. // Copyright © 2016年 ZC. All ...
- ural 1748 The Most Complex Number 和 丑数
题目:http://acm.timus.ru/problem.aspx?space=1&num=1748 题意:求n范围内约数个数最多的那个数. Roughly speaking, for a ...
- Number Transformation
Description In this problem, you are given a pair of integers A and B. You can transform any integer ...
- Almost Prime
Description Almost Prime time limit per test: 2 seconds memory limit per test: 256 megabytes input: ...
- USACO 1.5 Prime Palindromes
Prime Palindromes The number 151 is a prime palindrome because it is both a prime number and a palin ...
- Kattis之旅——Prime Reduction
A prime number p≥2 is an integer which is evenly divisible by only two integers: 1 and p. A composit ...
随机推荐
- Linux rndis_host 驱动的一个BUG与解决方案
关键字 rndis_host, linux, kernel, modem 综述 rndis 是微软定义的一套通讯方案.类似的协议还有 qmi/mbim/ecm/ncm. rndis 协议足够简单,可靠 ...
- CentOS6.10下安装MongoDB和Redis
安装mongodb 首先考虑离线安装,但是安装过程中在启动服务的时候出现了问题,centOS出于稳定原因考虑,系统自带的glibc版本过低, 而编译需要使用较高版本,这个问题我查询了一下,需要升级gl ...
- Linux实战(13):Centos8安装FFmpeg
此文章所做的操作参考漏网的鱼:参考链接 步骤1:安装RPMfusion Yum存储库 RHEL或兼容发行版(如CentOS)上启用EPEL. dnf -y install https://downlo ...
- Vue 登录/登出以及JWT认证
1. 后端代码概览 server/router/index.js 请求 router.get('/getUserInfo', function (req, res, next) { // 登录请求 r ...
- 每天一个dos命令-del.
比较常用的选项: /F 强制删除只读文件. /Q 安静模式.删除全局通配符时,不要求确认 文件名或者路径中有空格,需要使用引号包围 常用的实例:del /q/f c:\Securitylog\S ...
- Servlet中关于中文乱码
一.客户端请求服务器的数据有乱码 1.get方式请求 ①修改tomcat/conf/server.xml,在<Connector> 标签中添加属性useBodyEncodingForURI ...
- kafka面试总结
本文为复习期间面试总结 从以下方面对kafka面试进行总结:基本原理架构/项目实践/生产者/消费者/协调者/存储层/控制器 基本原理架构 简单讲下什么是kafka[一句话概括/架构图] 消息队列选型 ...
- python 报错 wxPyDeprecationWarning: Using deprecated class PySimpleApp.
如题:python 报错 提示为 : wxPyDeprecationWarning: Using deprecated class PySimpleApp. 解决:将 wx.PySimpleApp() ...
- Solon详解(十)- 怎么用 Solon 开发基于 undertow jsp tld 的项目?
Solon详解系列文章: Solon详解(一)- 快速入门 Solon详解(二)- Solon的核心 Solon详解(三)- Solon的web开发 Solon详解(四)- Solon的事务传播机制 ...
- MFC 简介
参考:https://baike.baidu.com/item/MFC/2236974 MFC (微软基础类库) 编辑 锁定 讨论999 MFC(Microsoft Foundation Clas ...