当前位置:网站首页>[day 29] given an integer, please find its factor number
[day 29] given an integer, please find its factor number
2022-07-03 01:16:00 【Stubble】
Learning Guide
order 、 Column Preface
This column opens , The purpose is to help everyone better master learning Java, Especially some Java Learners' It is difficult to find systematic algorithm learning materials on the Internet to help you get started with algorithms , At the same time, if you have any questions about the contents of the column, you can add my wechat at the end of the article to give you a one-to-one explanation .
But the most important thing is to think independently , For all the content of this column , Be able to fully master , Write the code completely by yourself , There must be no problem getting started with the algorithm .
The learning of algorithm must not be short of summary , Here I recommend that you can go to University algorithm community Punch in the learned knowledge , To consolidate and review .
The only way to learn algorithms well must be Topic sea strategy , Only by accumulating a lot of practice can you practice your skills . Any topic of the column I will start with 【 Title Description 】【 Their thinking 】【 Template code 】【 Code parsing 】 Wait for four plates to explain .
One 、【 Example 1】
1、 Title Description
Given an integer n ( 1 ≤ n ≤ 100000 ) n(1\le n \le100000) n(1≤n≤100000), Please find out the number of factors .
2、 Their thinking
- ( 1 ) (1) (1) We can think of the previous articles on judging prime numbers , Because the determination of prime number also depends on the factor number of a number , It was written at that time O ( n ) O(n) O(n) And a O ( n ) O(\sqrt n) O(n) How to do it .
3、 Template code
1.O(n) practice
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. Radical sign n practice
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. Theorem of the number of approximations
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、 Code parsing
- ( 1 ) (1) (1) The first two methods are not mentioned here , In the 5 5 5 It is explained in the prime number determination of days , Let's focus on the theorem of approximate numbers .
- ( 2 ) (2) (2) For a greater than 1 1 1 The integer of n n n, Decomposable prime factor
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
be n n n The positive divisors of are 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).
among a 1 、 a 2 、 a 3 . . . a k a_1、a_2、a_3...a_k a1、a2、a3...ak yes p 1 、 p 2 、 p 3 、 . . . p k p_1、p_2、p_3、...p_k p1、p2、p3、...pk The index of . - ( 3 ) (3) (3) Theorem proving : about n n n, We have : 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
about n n n Any divisor of d d d, Then there are 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, among ( 0 ≤ β i ≤ a i ) (0 \le \beta_i \leq a_i) (0≤βi≤ai), every last β i \beta_i βi The value of is in total ( a i + 1 ) individual (a_i+1) individual (ai+1) individual . Of each item β i \beta _i βi Different , Then the divisors are different . β 1 \beta _1 β1 The range is [ 0 , a 1 ] [0,a_1] [0,a1], β 2 \beta _2 β2 The range is [ 0 , a i ] [0,a_i] [0,ai], β k \beta _k βk The de value range of is [ 0 , a k ] [0,a_k] [0,ak]. - ( 4 ) (4) (4) According to the principle of multiplication , n n n The approximate number of 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)

3、 ... and 、 Recommendation column
Four 、 After-school exercises
| Serial number | Topic link | Difficulty rating |
|---|---|---|
| 1 | Ugly number | 2 |
边栏推荐
- 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
- 18_微信小程序之微信视频号滚动自动播放视频效果实现2.0
- 1696C. Fishingprince Plays With Array【思维题 + 中间状态 + 优化存储】
- [AUTOSAR eight OS]
- Esp32 simple speed message test of ros2 (limit frequency)
- Makefile中wildcard、patsubst、notdir的含义
- Database SQL language 02 connection query
- 比较版本号
- 按鍵精靈打怪學習-多線程後臺坐標識別
- 递归处理组织的几种情况
猜你喜欢

Foundations of data science is free to download

用Go+绘制爱心给心爱的她表白

ROS2之ESP32简单速度消息测试(极限频率)

What is needed to develop a domestic arm intelligent edge computing gateway

Find a benchmark comrade in arms | a million level real-time data platform, which can be used for free for life
![[Arduino experiment 17 L298N motor drive module]](/img/e2/4511eaa942e4a64c8ca2ee70162785.jpg)
[Arduino experiment 17 L298N motor drive module]

Strongly connected components of digraph

【FH-GFSK】FH-GFSK信号分析与盲解调研究

安全运营四要素之资产、脆弱性、威胁和事件

12_微信小程序之微信视频号滚动自动播放视频效果实现
随机推荐
leetcode:871. 最低加油次数【以前pat做过 + 最大堆 +贪心】
基本远程连接工具Xshell
SwiftUI 组件大全之使用 SceneKit 和 SwiftUI 构建交互式 3D 饼图(教程含源码)
Key wizard hit strange learning - automatic path finding back to hit strange points
Daily topic: movement of haystack
Draw love with go+ to express love to her beloved
正确甄别API、REST API、RESTful API和Web Service之间的异同
2022 coal mine gas drainage examination question bank and coal mine gas drainage examination questions and analysis
Button wizard play strange learning - go back to the city to buy medicine and add blood
【无标题】
[shutter] image component (configure local GIF image resources | load placeholder with local resources)
matlab查找某一行或者某一列在矩阵中的位置
1038 Recover the Smallest Number
Makefile中wildcard、patsubst、notdir的含义
MySQL
2022 Jiangxi Provincial Safety Officer B certificate reexamination examination and Jiangxi Provincial Safety Officer B certificate simulation examination question bank
R language ggplot2 visualization: use ggplot2 to display dataframe data that are all classified variables in the form of thermal diagram, and customize the legend color legend of factor
[AUTOSAR 11 communication related mechanism]
kivy教程之在 Kivy App 中使用 matplotlib 的示例
465. 最优账单平衡 DFS 回溯