简体   繁体   English

寻找最大的阶乘n,intiger允许! C ++

[英]Finding maxium factorial n what intiger allows! C++

Sorry for bad English. 对不起,英语不好。
So my problem is that i need to find all possible factorials starting from 1. I need it to stop when Int have maximum memory used and print out maximum factorial value. 所以我的问题是我需要找到从1开始的所有可能的阶乘。当Int使用了最大的内存并打印出最大阶乘值时,我需要它停止。 My code is pretty simple, but i do not know how to get to stop loop when it reach maximum of Intiger values. 我的代码非常简单,但是当达到最大Intiger值时,我不知道如何停止循环。

 #include <iostream>
 #include<climits>//
 #include <cmath>
 using namespace std;

 int main() {

int k,n=0;
unsigned  int factorial = 1;
unsigned  int factorial2=1;
unsigned  uval=INT_MAX;

cout << "Ievadi koeficentu k: ";
cin >> k;
for(int i = 1; i<=k; ++i) {
    factorial *= i;
}
cout << "Ievadita koeficenta " << k << " faktorials " << " = " <<factorial;

cout << "\nVisi iespejamie faktoriali no 1 - n: ";
for(int s = 1; s<=uval; ++s) {

    factorial2 *= s;
   if( s < uval / factorial2 ){
      cout  <<" \nkoeficenta " << s << " faktorials ir ==> " <<factorial2;
   }
}




return 0;
}

The problem is that the loop is going to calculate factorial to all UINT_MAX values, and most of the output will be 0 becouse memory is overloaded. 问题在于,循环将对所有UINT_MAX值进行阶乘计算,并且由于内存过载,大部分输出将为0。 But it should stop before it goes bigger that UINT_MAX memory! 但是它应该停止,直到它超过UINT_MAX内存! Hope you all understand my problem and will help me with this. 希望大家都理解我的问题,并会对此有所帮助。

If factorial2 > UINT_MAX / (s+1) , the next factorial can't be calculated. 如果factorial2 > UINT_MAX / (s+1) ,则无法计算下一个阶乘。

By the way, you should use unsigned int for factorial2 顺便说一句,您应该对factorial2使用unsigned int

Before calculating the next factorial in the list, see if the previous one is greater than UINT_MAX / i . 在计算列表中的下一个阶乘之前,请查看上一个阶乘是否大于UINT_MAX / i If it is, you know that the next multiplication will go out of bounds. 如果是这样,您知道下一个乘法将超出范围。

In a 32-bit signed integer, 12! 在32位有符号整数中,为12! is the largest possible. 是最大的可能。

You can check this by doing 您可以通过执行此操作来检查

if (INT_MAX / fact_so_far < n)
{
   std::cout << "Max factorial " << n-1 << std::endl;
}

[This code STOPS when it reaches the "unable to calculate", because INT_MAX / fact_so_far will not multiply without overflow]. [此代码在达到“无法计算”时INT_MAX / fact_so_far ,因为INT_MAX / fact_so_far将不会相乘而不会溢出)。

if (INT_MAX / fact_so_far >= n)
{
    fact_so_far *= n;
}
else
{
    std::cout << n << " is too large to calculate factorial" << std::endl;
}

would be the other way to do this. 这将是另一种方式。 [Obviously with suitable loop to increment n ] [显然有合适的循环来递增n ]

Not that MAX_UINT is the max value for unsigned int , not for int [it's typically half that]. 并不是说MAX_UINTunsigned int的最大值,而不是int的最大值(通常是它的一半)。

Edit to explain the logic: 编辑以解释逻辑:

The logic here is that if we divide INT_MAX with what our current factorial value is, it should produce a value larger than n [the current multiplier for the next factorial]. 这里的逻辑是,如果将INT_MAX除以当前阶乘值是什么,它将产生一个大于n [下一个阶乘的当前乘数]的值。

As a simple step through example, we pick a MAX_INT of 127: 作为简单的示例,我们选择MAX_INT为127:

Initial state: 初始状态:

factorial = 1, n = 1; 

Steps: 脚步:

n = 2, MAX_INT / factorial = 127 -> factorial *= n => 2
n = 3, MAX_INT / factorial = 63 -> factorial *= n => 6
n = 4, MAX_INT / factorial = 21 -> factorial *= n => 24
n = 5, MAX_INT / factorial = 5 -> factorial *= n = 120
n = 6, MAX_INT / factorial = 1 -> FAIL - will overflow.

您正在将带符号的int与无符号的int进行比较,即您正在执行i <UINT_MAX(这是无符号的int的最大值),这是错误的,将导致溢出和错误的条件检查。

so i finaly did this with my code. 所以我最终用我的代码做到了这一点。 Now it works but its not realy good. 现在它可以工作了,但不是很好。 Maybe someone someday will need something like this as a start for his own max factorial programm. 也许某天某人会需要这样的东西作为他自己的最大阶乘编程的开始。 First program will ask you to insert a random number and then it calculate its factorial (only in max int borders). 第一个程序将要求您插入一个随机数,然后计算其阶乘(仅在最大int边界内)。 Then it prints out all possible int factorials. 然后打印出所有可能的int阶乘。 Its show only 11! 它的节目只有11! as max but the max is 12! 作为最大,但最大为12! I add factorial 12! 我添加阶乘12! manualy, becouse the if statment cannot print out excatly the number 12 as max it prints one before. 手动操作,因为if语句不能完全打印出数字12,因为最多只能打印一次。 And in the end the programm shows you what is maxium possible factorial for intiger. 最后,程序将向您展示什么是最大的因果图因数。

#include <iostream>
#include<climits>//
#include <cmath>
using namespace std;

int main() {
int k, max,s;
unsigned  int factorial = 1;
unsigned  int factorial2 = 1;
unsigned  uval=INT_MAX;

cout << "Ievadi koeficentu k: ";
cin >> k;
for(int i = 1; i<=k; ++i) {
    factorial *= i;
}
cout << "Ievadita koeficenta " << k << " faktorials " << " = " <<factorial;

cout << "\nVisi iespejamie faktoriali no 1 - n: ";
for( s = 1; s<=uval; ++s) {

    factorial2 *= s;
    max=factorial2;

if( s <= uval / factorial2 ){
cout  <<" \nkoeficenta " << s << " faktorials ir ==> " <<factorial2;

}
else {
        break;
}
    }
cout  <<" \nkoeficenta " << s << " faktorials ir ==> " <<factorial2;
cout  <<" \nMaksimalais faktorials ir skaitla " << s << " faktorials ==> " <<max;



return 0;
}

This is what the programm looks like! 这就是程序的样子!

程式化 ![Programm][1] ![程序] [1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM