如何在c++中找到一系列在给定范围内有5个或更多因子的数
how to find series of a numbers those have 5 or more factors in a given range in c++
#include使用命名空间std;
int main()
{
int n, i;
cout << "Enter a positive integer: ";
cin >> n;
cout << "Factors of " << n << " are: " << endl;
for(i = 1; i <= n; ++i)
{
if(n % i == 0)
cout << i << endl;
}
return 0;
}
我理解下面的求数因子的问题。但我想做一个c++程序,它只显示有5个或更多因子的数字。假设我给出的数字范围是15到20,那么它只会打印那些有5个或更多因子的数字。例如,如果我给出一个15到20的范围,那么它只会打印出16,18,20,因为这3个整数在15到20范围内有5个或更多的因子。我不知道该怎么做那个代码,所以我在问
据我所知,您正在搜索自然数的素数因子。首先,你发布的代码是为了得到给定正数的所有除数。但是发现它的素数有点不同,但想法与你使用的相同(模块算术(
这是实现任务的一个非常简单的版本(但需要优化(
#include <iostream>
//This function does not handle the repeating factors count
int numberOfPrimeFactors(int number) {
int count = 0;
for ( int i = 2; i <= number; ++i ) {
while ( number % i == 0 ) {
number /= i;
count++;
}
}
return count;
}
int main() {
int Rbegin = 1;
int Rend = 100;
for(int i = Rbegin; i<Rend; ++i) {
if(numberOfPrimeFactors(i) >= 5)
std::cout << i << " has 5 or more prime factor"<< std::endl;
}
}
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 如何在C++中从两个单独的for循环中添加两个数组
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 处理多个异常集合的C++方法
- 什么时候在C++中返回常量引用是个好主意
- 给定n个元素的m个集合.在C++中找到出现在最大集合数中的元素
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- 尝试通过多个向量访问变量时,向量下标超出范围
- Ardunio UNO解决了多个重叠的定时器循环
- 如何返回一个类的两个对象相加的结果
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 数到第n个楼梯的路(顺序无关紧要)
- 基于多个条件处理地图中的所有元素
- 为多个会话保留XPtr
- 如何在C++中初始化嵌套类中的2个memeber
- 运算符C++ "delete []"仅删除 2 个前值
- IPC使用多个管道和分支进程来运行Python程序