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的更多相关文章

  1. 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 ...

  2. 最小公倍数 SRM 661 Div1 250: MissingLCM

    Problem Statement The least common multiple (denoted "lcm") of a non-empty sequence of pos ...

  3. 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. 4_Is Prime

    4 // // ViewController.swift // Is Prime // // Created by ZC on 16/1/9. // Copyright © 2016年 ZC. All ...

  5. ural 1748 The Most Complex Number 和 丑数

    题目:http://acm.timus.ru/problem.aspx?space=1&num=1748 题意:求n范围内约数个数最多的那个数. Roughly speaking, for a ...

  6. Number Transformation

    Description In this problem, you are given a pair of integers A and B. You can transform any integer ...

  7. Almost Prime

    Description Almost Prime time limit per test: 2 seconds memory limit per test: 256 megabytes input: ...

  8. USACO 1.5 Prime Palindromes

    Prime Palindromes The number 151 is a prime palindrome because it is both a prime number and a palin ...

  9. Kattis之旅——Prime Reduction

    A prime number p≥2 is an integer which is evenly divisible by only two integers: 1 and p. A composit ...

随机推荐

  1. maoge数

    maoge数 题目描述 maoge定义一个数x是maoge数的条件,当且仅当x的各数位之和等于 x / 2向下取整,现在maoge想让你求 n 的约数中有多少个maoge数 输入格式 输入一个数 n ...

  2. SpringCloud实战 | 第二篇:SpringCloud整合Nacos实现注册中心

    前言 随着eureka的停止更新,如果同时实现注册中心和配置中心需要SpringCloud Eureka和SpringCloud Config两个组件;配置修改刷新时需要SpringCloud Bus ...

  3. Vue中你忽略的点

    自定义组件使用 v-model <my-component v-model="data"></my-component> 在组件my-component中, ...

  4. 吾日三省吾身 java核心代码 高并发集群 spring源码&思想

    阿里面试题    未解决https://my.oschina.net/wuweixiang/blog/1863322 java基础  有答案  https://www.cnblogs.com/xdp- ...

  5. 【Netty之旅四】你一定看得懂的Netty客户端启动源码分析!

    前言 前面小飞已经讲解了NIO和Netty服务端启动,这一讲是Client的启动过程. 源码系列的文章依旧还是遵循大白话+画图的风格来讲解,本文Netty源码及以后的文章版本都基于:4.1.22.Fi ...

  6. JVM学习(五)对象的引用类型

    一.引言 前面我们学习了JVM的垃圾回收机制,我们知道了垃圾回收是JVM的自发行为:虽然我们可以通过System.gc() 或Runtime.getRuntime().gc()进行显式调用垃圾回收 , ...

  7. MyEclipse中的项目导入到Eclipse中运行的错误解决

    之前用的myEclipse,后来把项目导入eclipse发现报错,将MyEclipse中的项目导入到Eclipse中运行,不注意一些细节,会造成无法运行的后果.下面就说说具体操作:导入后出现如下错误: ...

  8. python中生成随机整数(random模块)

    1.从一个序列中随机选取一个元素返回:   random.choice(sep)    2.用于将一个列表中的元素打乱   random.shuffle(sep)    3.在sep列表中随机选取k个 ...

  9. LDA主题模型困惑度计算

    对于LDA模型,最常用的两个评价方法困惑度(Perplexity).相似度(Corre). 其中困惑度可以理解为对于一篇文章d,所训练出来的模型对文档d属于哪个主题有多不确定,这个不确定成都就是困惑度 ...

  10. Dominate【操作系统的经典算法】

    此篇文章我们来谈一谈操作系统中都出现过哪些算法,请欣赏下图 ↓ 进程和线程管理中的算法 进程和线程在调度时候出现过很多算法,这些算法的设计背景是当一个计算机是多道程序设计系统时,会频繁的有很多进程或者 ...