Summary: Prime

本文总结了关于素数的定义、性质以及如何高效地找出给定数字的所有素因数的方法。此外,还提供了判断自然数是否为素数的函数实现,并介绍了通过埃拉托斯特尼筛法找出小于等于给定整数的所有素数的算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近遇到很多问题都跟Prime有关,于是总结一下:

Prime definition:A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. A natural number greater than 1 that is not a prime number is called a composite number. So 1 is not a prime, but 2 is a prime.

性质:

先说composite number:Every composite number has at least one prime factor less than or equal to square root of itself. composite又必须含有divisor other than 1 and itself, 所以必须在[2 ,\sqrt{n}] inclusive之间有可以整除的prime factor

所以prime number的性质就是:在 [2 ,\sqrt{n}] inclusive 之间找不到一个数i 使得 n % i == 0

 

 

问题1:Efficient program to print all prime factors of a given number

Given a number n, write an efficient function to print all prime factors of n. For example, if the input number is 12, then output should be “2 2 3″. And if the input number is 315, then output should be “3 3 5 7″.

Following are the steps to find all prime factors.
1) While n is divisible by 2, print 2 and divide n by 2.
2) After step 1, n must be odd. Now start a loop from i = 3 to square root of n. While i divides n, print i and divide n by i, increment i by 2 and continue.
3) If n is a prime number and is greater than 2, then n will not become 1 by above two steps. So print n if it is greater than 2.

 1 // A function to print all prime factors of a given number n
 2 void primeFactors(int n)
 3 {
 4     // Print the number of 2s that divide n
 5     while (n%2 == 0)
 6     {
 7         printf("%d ", 2);
 8         n = n/2;
 9     }
10  
11     // n must be odd at this point.  So we can skip one element (Note i = i +2)
12     for (int i = 3; i <= sqrt(n); i = i+2)
13     {
14         // While i divides n, print i and divide n
15         while (n%i == 0)
16         {
17             printf("%d ", i);
18             n = n/i;
19         }
20     }
21  
22     // This condition is to handle the case whien n is a prime number
23     // greater than 2
24     if (n > 2)
25         printf ("%d ", n);
26 }

测试一下上述程序

int main()
{
    int n = 315;
    primeFactors(n);
    return 0;
}

输出

3 3 5 7

要特别注意12行sqrt(n)中的n是一直随 n = n / i 更新的,而不是始终都是最初始的n。这相当于是在找新的n的prime factor, 而且确定这个新的n的prime factor不包括之前找到过的 i,因为前面已经把小的prime factor全找出来了。反正只要 n 还是 composite number, 那么一定有一个prime factor小于sqrt(n), 那么就一定能找到新的 i 满足条件。找不到就说明 n 不是 composite number了。所以,最后跳出while循环是因为要么n变成了1,要么n变成了质数

当然不这么设置 i 的范围也可以,可以设置 i 从3 到最初的sqrt(n), 或者甚至n,这样可以,但是浪费时间。

How does this work?
The steps 1 and 2 take care of composite numbers and step 3 takes care of prime numbers. To prove that the complete algorithm works, we need to prove that steps 1 and 2 actually take care of composite numbers. This is clear that step 1 takes care of even numbers. And after step 1, all remaining prime factor must be odd (difference of two prime factors must be at least 2), this explains why i is incremented by 2.
Now the main part is, the loop runs till square root of n not till. To prove that this optimization works, let us consider the following property of composite numbers.
Every composite number has at least one prime factor less than or equal to square root of itself.
This property can be proved using counter statement. Let a and b be two factors of n such that a*b = n. If both are greater than \sqrt{n}, then a.b > \sqrt{n} * \sqrt{n} which contradicts the expression “a * b = n”.

In step 2 of the above algorithm, we run a loop and do following in loop
a) Find the least prime factor i (must be less than \sqrt{n})
b) Remove all occurrences i from n by repeatedly dividing n by i.
c) Repeat steps a and b for divided n and i = i + 2. The steps a and b are repeated till n becomes either 1 or a prime number.

 

 

问题2: 写一个判断自然数n是不是prime的函数

 1 public class Solution {
 2     public boolean isPrime(int n) {
 3         if (n <= 1) return false;
 4         if (n == 2) return true;
 5         if (n % 2 == 0) return false;
 6         for (int i=3; i<=Math.sqrt(n); i=i+2) {
 7             if (n % i == 0) return false;
 8         }
 9         return true;
10     }
11 }

Wiki上也给出了这个算法:The property of being prime (or not) is called primality. A simple but slow method of verifying the primality of a given number n is known as trial division. It consists of testing whether n is a multiple of any integer between 2 and \sqrt{n}. Algorithms much more efficient than trial division have been devised to test the primality of large numbers. Particularly fast methods are available for numbers of special forms, such as Mersenne numbers. As of April 2014, the largest known prime number has 17,425,170 decimal digits.

 

 

问题3: 

Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number.
For example, if n is 10, the output should be “2, 3, 5, 7″. If n is 20, the output should be “2, 3, 5, 7, 11, 13, 17, 19″.

The sieve of Eratosthenes is one of the most efficient ways to find all primes smaller than n when n is smaller than 10 million or so (Ref Wiki).

Following is the algorithm to find all the prime numbers less than or equal to a given integer n by Eratosthenes’ method:

  1. Create a list of consecutive integers from 2 to n: (2, 3, 4, …, n).
  2. Initially, let p equal 2, the first prime number.
  3. Starting from p, count up in increments of p and mark each of these numbers greater than p itself in the list. These numbers will be 2p, 3p, 4p, etc.; note that some of them may have already been marked.
  4. Find the first number greater than p in the list that is not marked. If there was no such number, stop. Otherwise, let p now equal this number (which is the next prime), and repeat from step 3.

When the algorithm terminates, all the numbers in the list that are not marked are prime.

 1 #include <stdio.h>
 2 #include <string.h>
 3  
 4 // marks all mutiples of 'a' ( greater than 'a' but less than equal to 'n') as 1.
 5 void markMultiples(bool arr[], int a, int n)
 6 {
 7     int i = 2, num;
 8     while ( (num = i*a) <= n )
 9     {
10         arr[ num-1 ] = 1; // minus 1 because index starts from 0.
11         ++i;
12     }
13 }
14  
15 // A function to print all prime numbers smaller than n
16 void SieveOfEratosthenes(int n)
17 {
18     // There are no prime numbers smaller than 2
19     if (n >= 2)
20     {
21         // Create an array of size n and initialize all elements as 0
22         bool arr[n];
23         memset(arr, 0, sizeof(arr));
24  
25         /* Following property is maintained in the below for loop
26            arr[i] == 0 means i + 1 is prime
27            arr[i] == 1 means i + 1 is not prime */
28         for (int i=1; i<n; ++i)
29         {
30             if ( arr[i] == 0 )
31             {
32                 //(i+1) is prime, print it and mark its multiples
33                 printf("%d ", i+1);
34                 markMultiples(arr, i+1, n);
35             }
36         }
37     }
38 }
39  
40 // Driver Program to test above function
41 int main()
42 {
43     int n = 30;
44     printf("Following are the prime numbers below %d\n", n);
45     SieveOfEratosthenes(n);
46     return 0;
47 }

 

资源下载链接为: https://pan.quark.cn/s/1bfadf00ae14 “STC单片机电压测量”是一个以STC系列单片机为基础的电压检测应用案例,它涵盖了硬件电路设计、软件编程以及数据处理等核心知识点。STC单片机凭借其低功耗、高性价比和丰富的I/O接口,在电子工程领域得到了广泛应用。 STC是Specialized Technology Corporation的缩写,该公司的单片机基于8051内核,具备内部振荡器、高速运算能力、ISP(在系统编程)和IAP(在应用编程)功能,非常适合用于各种嵌入式控制系统。 在源代码方面,“浅雪”风格的代码通常简洁易懂,非常适合初学者学习。其中,“main.c”文件是程序的入口,包含了电压测量的核心逻辑;“STARTUP.A51”是启动代码,负责初始化单片机的硬件环境;“电压测量_uvopt.bak”和“电压测量_uvproj.bak”可能是Keil编译器的配置文件备份,用于设置编译选项和项目配置。 对于3S锂电池电压测量,3S锂电池由三节锂离子电池串联而成,标称电压为11.1V。测量时需要考虑电池的串联特性,通过分压电路将高电压转换为单片机可接受的范围,并实时监控,防止过充或过放,以确保电池的安全和寿命。 在电压测量电路设计中,“电压测量.lnp”文件可能包含电路布局信息,而“.hex”文件是编译后的机器码,用于烧录到单片机中。电路中通常会使用ADC(模拟数字转换器)将模拟电压信号转换为数字信号供单片机处理。 在软件编程方面,“StringData.h”文件可能包含程序中使用的字符串常量和数据结构定义。处理电压数据时,可能涉及浮点数运算,需要了解STC单片机对浮点数的支持情况,以及如何高效地存储和显示电压值。 用户界面方面,“电压测量.uvgui.kidd”可能是用户界面的配置文件,用于显示测量结果。在嵌入式系统中,用
### PrimeTime DMSA Usage and Configuration PrimeTime (PT) is a static timing analysis tool used in the design flow to analyze and optimize the timing of digital circuits. The DMSA (Design Margin for Statistical Analysis) feature in PrimeTime focuses on improving timing closure by analyzing multiple scenarios simultaneously, ensuring better accuracy and convergence during the physical implementation phase. #### Key Concepts and Features 1. **DMSA Workflow**: In the ARM A75 PrimeTime DMSA golden flow, the process differs from traditional DMSA flows because it incorporates the DEF file generated during the APR (Automatic Place and Route) stage[^1]. This allows PrimeTime to visualize empty spaces within the design, enabling more precise adjustments to timing during the placement refinement phase. As a result, the maximum movement distance (`max move`) during placement refinement is minimized, ensuring that post-timing ECO (Engineering Change Order) results closely match the DMSA analysis outcomes[^1]. 2. **Timing Reports**: The DMSA timing reports in PrimeTime are divided into two parts: individual scenario reports and multi-scenario DMSA reports[^2]. The single-scenario reports provide detailed insights into timing violations for each specific condition, while the multi-scenario report aggregates this information across all scenarios. To avoid issues, it is recommended to generate single-scenario reports first (using the `remote_execute` command), followed by the DMSA multi-scenario report[^2]. #### Example Script for DMSA Timing Report Generation Below is an example script demonstrating how to generate DMSA timing reports in PrimeTime: ```tcl # Load the design read_def /path/to/design.def read_lib /path/to/library.lib link_design top # Generate single-scenario timing reports foreach scenario [get_scenarios] { report_timing -scenario $scenario -file ${scenario}_timing.rpt } # Generate DMSA multi-scenario timing report report_dmsa -file dmsa_summary.rpt ``` This script performs the following tasks: - Loads the DEF file and library. - Links the top-level design. - Iterates through each scenario to generate individual timing reports. - Generates a consolidated DMSA report summarizing timing violations across all scenarios. #### Best Practices for PrimeTime DMSA Usage - Ensure that the DEF file is up-to-date and reflects the latest placement data[^1]. - Use the `max move` constraint effectively to limit cell movements during placement refinement, thereby maintaining the integrity of the timing analysis[^1]. - Validate the accuracy of single-scenario reports before proceeding to the multi-scenario DMSA report generation[^2]. ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值