当前位置:网站首页>【第29天】给定一个整数,请你求出它的因子数
【第29天】给定一个整数,请你求出它的因子数
2022-07-03 00:51:00 【执 梗】
序、专栏前言
本专栏开启,目的在于帮助大家更好的掌握学习Java,特别是一些Java学习者难以在网上找到系统地算法学习资料帮助自身入门算法,同时对于专栏内的内容有任何疑问都可在文章末尾添加我的微信给你进行一对一的讲解。
但最最主要的还是需要独立思考,对于本专栏的所有内容,能够进行完全掌握,自己完完全全将代码写过一遍,对于算法入门肯定是没有问题的。
算法的学习肯定不能缺少总结,这里我推荐大家可以到高校算法社区将学过的知识进行打卡,以此来进行巩固以及复习。
学好算法的唯一途径那一定是题海战略,大量练习的堆积才能练就一身本领。专栏的任何题目我将会从【题目描述】【解题思路】【模板代码】【代码解析】等四板块进行讲解。
一、【例题1】
1、题目描述
给定一个整数 n ( 1 ≤ n ≤ 100000 ) n(1\le n \le100000) n(1≤n≤100000),请你求出它的因子数量。
2、解题思路
- ( 1 ) (1) (1)我们可以联想到之前判断质数的文章,因为质数的判定也是看一个数的因子数,那时就已经写出了 O ( n ) O(n) O(n)和一个 O ( n ) O(\sqrt n) O(n)的做法。
3、模板代码
1.O(n)做法
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int ans=0;
for (int i = 1; i <=n; i++) {
if (n%i==0) ans++;
}
System.out.println(ans);
}
}
2.根号n做法
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int ans=0;
for (int i = 1; i <=n/i; i++) {
if (n%i==0){
if (i!=n/i) ans+=2;
else ans++;
}
}
System.out.println(ans);
}
}
3.约数个数定理
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
System.out.println(getsum(n));
}
static int getsum(int n){
int ans=1;
for (int i = 2; i <=n/i; i++) {
if (n%i==0){
int a=0;
while (n%i==0){
a++;
n/=i;
}
ans*=(a+1);
}
}
if (n>1) ans*=2;
return ans;
}
}
4、代码解析
- ( 1 ) (1) (1)前两种方法在这不讲,在第 5 5 5天的质数判定中讲解过,重点讲一下约数个数定理。
- ( 2 ) (2) (2)对于一个大于 1 1 1的整数 n n n,可以分解质因数
n = ∏ i = 1 k p i a i = p 1 a 1 × p 2 a 2 × p 3 a 3 . . . . . . p k a k n=\prod_{i=1}^{k} p_i^{a^i}=p_1^{a^1}\times p_2^{a^2}\times p_3^{a^3}......p_k^{a^k} n=i=1∏kpiai=p1a1×p2a2×p3a3......pkak
则 n n n的正约数个数为 f ( n ) = ∏ i = 1 k ( a i + 1 ) = ( a 1 + 1 ) ( a 2 + 1 ) . . . ( a k + 1 ) 。 f(n)=\prod_{i=1}^{k}(a_i+1)=(a_1+1)(a_2+1)...(a_k+1)。 f(n)=∏i=1k(ai+1)=(a1+1)(a2+1)...(ak+1)。
其中 a 1 、 a 2 、 a 3 . . . a k a_1、a_2、a_3...a_k a1、a2、a3...ak是 p 1 、 p 2 、 p 3 、 . . . p k p_1、p_2、p_3、...p_k p1、p2、p3、...pk的指数。 - ( 3 ) (3) (3)定理证明:对于 n n n,我们有: n = p 1 a 1 × p 2 a 2 × p 3 a 3 . . . . . . p k a k n=p_1^{a^1}\times p_2^{a^2}\times p_3^{a^3}......p_k^{a^k} n=p1a1×p2a2×p3a3......pkak
对于 n n n的任意一个约数 d d d,则有 d = p 1 β 1 × p 2 β 2 × p 3 β 3 . . . . . . p k β k d=p_1^{^\beta1}\times p_2^{\beta ^2}\times p_3^{\beta ^3}......p_k^{\beta^k} d=p1β1×p2β2×p3β3......pkβk,其中 ( 0 ≤ β i ≤ a i ) (0 \le \beta_i \leq a_i) (0≤βi≤ai),每一个 β i \beta_i βi的取值总共有 ( a i + 1 ) 个 (a_i+1)个 (ai+1)个。每一项的 β i \beta _i βi不同,则约数就不相同。 β 1 \beta _1 β1的范围是 [ 0 , a 1 ] [0,a_1] [0,a1], β 2 \beta _2 β2的范围是 [ 0 , a i ] [0,a_i] [0,ai], β k \beta _k βk的去取值范围是 [ 0 , a k ] [0,a_k] [0,ak]。 - ( 4 ) (4) (4)根据乘法原理, n n n的约数个数 f ( n ) = ( a 1 + 1 ) ( a 2 + 1 ) . . . ( a k + 1 ) f(n)=(a_1+1)(a_2+1)...(a_k+1) f(n)=(a1+1)(a2+1)...(ak+1)

三、推荐专栏
四、课后习题
| 序号 | 题目链接 | 难度评级 |
|---|---|---|
| 1 | 丑数 | 2 |
边栏推荐
- First hand evaluation of Reza electronics rz/g2l development board
- Asynchronous, email and scheduled tasks
- Niu Ke swipes questions and clocks in
- Foundations of data science is free to download
- Key wizard play strange learning - front desk and Intranet send background verification code
- Leetcode-849: maximum distance to the nearest person
- 信息熵的基础
- 18_微信小程序之微信视频号滚动自动播放视频效果实现2.0
- Kivy教程大全之如何在 Kivy 中创建下拉列表
- [AUTOSAR eight OS]
猜你喜欢

Explain the basic concepts and five attributes of RDD in detail
![[AUTOSAR II appl overview]](/img/da/76ccc05e2199705b20d8304bfb86b2.png)
[AUTOSAR II appl overview]
![[AUTOSAR nine c/s principle Architecture]](/img/59/ce32c0ff58ef5d8385fe950136175b.png)
[AUTOSAR nine c/s principle Architecture]

Leetcode-2280: represents the minimum number of line segments of a line graph

基本远程连接工具Xshell

Lu Zhe, chief scientist of Shiping information: building data and personnel centered security capabilities
![[C language] branch and loop statements (Part 1)](/img/47/6efcc59bd26e26f66c698635c26c8b.png)
[C language] branch and loop statements (Part 1)

飞凌搭载TI AM62x的ARM核心板/开发板首发上市,亮相Embedded World 2022

RISA rz/g2l processor introduction | frame diagram | power consumption | schematic diagram and hardware design guide

Basic use of sringcloud & use of component Nacos
随机推荐
Explain the basic concepts and five attributes of RDD in detail
excel表格计算时间日期的差值,并转化为分钟数
18_微信小程序之微信视频号滚动自动播放视频效果实现2.0
Matlab Doppler effect produces vibration signal and processing
Cut point of undirected graph
JS inheritance and prototype chain
[overview of AUTOSAR four BSW]
R language uses coin package to apply permutation tests to independence problems (permutation tests, whether response variables are independent of groups, are two numerical variables independent, and
2022.2.14 resumption
Daily topic: movement of haystack
R language ggplot2 visual faceting, visual facet_wrap bar plot, using strip Text function customize the size of the strip of each facet title in the facet graph (cutimi
[AUTOSAR five methodology]
[AUTOSAR II appl overview]
Excel calculates the difference between time and date and converts it into minutes
安全运营四要素之资产、脆弱性、威胁和事件
excel去除小数点后面的数据,将数字取整
FPGA - 7系列 FPGA内部结构之Clocking -04- 多区域时钟
Telephone network problems
The R language uses the ctree function in the party package to build conditional inference decision trees, uses the plot function to visualize the trained conditional inference decision tree, and the
Lu Zhe, chief scientist of Shiping information: building data and personnel centered security capabilities