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. [LeetCode]678. 有效的括号字符串、20. 有效的括号(栈)

    题目 678. 有效的括号字符串 给定一个只包含三种字符的字符串:( ,) 和 *,写一个函数来检验这个字符串是否为有效字符串.有效字符串具有如下规则: 任何左括号 ( 必须有相应的右括号 ). 任何 ...

  2. HBase表的数据导出和导入

    1. 表数据导出 hbase org.apache.hadoop.hbase.mapreduce.Export test file:///home/hadoop/test (导入到本地) hbase ...

  3. 14个Spring MVC顶级技巧,随时用随时爽,一直用一直爽~

    通常,在Spring MVC中,我们编写一个控制器类来处理来自客户端的请求.然后,控制器调用业务类来处理与业务相关的任务,然后将客户端重定向到逻辑视图名称,该名称由Spring的调度程序Servlet ...

  4. 工作10年后,再看String s = new String("xyz") 创建了几个对象?

    这个问题相信每个学习java的同学都不陌生,作为一个经典的面试题,到现在工作这么多年了我真是认为挺操蛋的一个问题,在网上到现在你仍然可以看见很多讨论这个问题的人,其中不乏工作很多年的人都有争论,我认为 ...

  5. 人工智能顶级会议最佳论文里的“DaDianNao”是什么鬼?

    最近对人工智能领域的 AI 加速芯片感兴趣,在翻阅 Google 的第一代 TPU 论文时,在相关工作中看到了 DaDianNao,PuDianNao,ShiDianNao.看的我一脸懵逼,这是什么? ...

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

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

  7. axios之post提交

    axios官网地址:https://github.com/axios/axios post提交到后台需要做相对应的处理 使用URLSearchParams可以让post 数据提交到后台 对应gitHu ...

  8. 本地保存cookie

    原文https://blog.csdn.net/qq_29663071/article/details/73826642 https://www.cnblogs.com/webcome/p/54709 ...

  9. macOS使用ABP.vNext Core开发CMS系统(一) 让程序跑起来

    macOS使用ABP.vNext Core开发CMS系统(一) 让程序跑起来--2020年10月5日 国庆假期,陪老婆的同时也不能忘记给自己充充电,这不想搞个CMS系统,考虑自己的时间并不多,所以想找 ...

  10. HTML中css水平居中的几种方式

    1. 子元素为行内元素时,父元素使用 text-align: center; 实现子元素的水平居中: 2. 子元素为块级元素时, 2.1. 将子元素设置 margin: 0 auto; 实现居中: 2 ...