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 ...
随机推荐
- 吴恩达《深度学习》-第一门课 (Neural Networks and Deep Learning)-第二周:(Basics of Neural Network programming)-课程笔记
第二周:神经网络的编程基础 (Basics of Neural Network programming) 2.1.二分类(Binary Classification) 二分类问题的目标就是习得一个分类 ...
- [POJ3253]Fence Repair(单调队列)
题目链接 http://poj.org/problem?id=3253 题目描述 大意:切长度为a的木条的花费是a,给定最终切好的n段各自的长度,问由原来的一根木条(长度为n段长度和)以最终总花费最小 ...
- k8s报错解决思路
问题1 1.报错信息如下 [root@ken1 ~]# kubectl get po The connection to the server 192.168.64.11:6443 was refus ...
- 仿苏宁移动web页面 自适应 rem&less
index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- Python记录日志模块推荐-loguru!
作者:小张学Python 本文链接: https://mp.weixin.qq.com/s/dkNkEohPl6H2VopUrpxxZg 转载请注明来源!! 前言 在做项目的时候一直在用Pytho ...
- JDK动态代理学习心得
JDK动态代理是代理模式的一种实现方式,其只能代理接口.应用甚为广泛,比如我们的Spring的AOP底层就有涉及到JDK动态代理(此处后面可能会分享) 1.首先来说一下原生的JDK动态代理如何实现: ...
- 关于python中break与continue的区别
在python中break和continue都有跳出循环体的作用,但是他们还是有一些区别的,具体区别如下: break:是直接跳出循环,跳出自己所处的整个循环体 continue:只是跳出本次循环,而 ...
- 黑菜菌的JAVA学习笔记
简介 本文是笔者对<JAVA编程思想>的学习笔记.以自己的思维理解来写下这篇文章,尽可能地简练,易懂.本文将随本人学习进度实时更新 对象导论 抽象过程 汇编语言是对底层机器码的抽象,而面向 ...
- UnityShader学习笔记- Stencil Buffer
模板测试(Stencil Test)是现代渲染流水线的一环,其中涉及到的就是模板缓冲(Stencil Buffer),模板缓冲可以用来制作物体的遮罩.轮廓描边.阴影.遮挡显示等等效果 目录 Stenc ...
- is_mobile()判断手机移动设备
is_mobile()判断手机移动设备.md is_mobile()判断手机移动设备 制作响应式主题时会根据不同的设备推送不同的内容,是基于移动设备网络带宽压力,避免全局接收pc端内容. functi ...