本帖最后由 jackz007 于 2024-1-26 18:13 编辑 #include <stdio.h>
unsigned long long common(unsigned long long a , unsigned long long b)
{
unsigned long long c , e , r = 0 ;
if(0xffffffffffffffff / a >= b) {
e = a * b ;
while(b) {
c = a % b ;
a = b ;
b = c ;
}
r = e / a ;
}
return r ;
}
int main(void)
{
unsigned long long c , d , e ;
int f ;
for(c = d = 2 , f = 1 ; c < 2024 ; c ++) {
if((e = common(d , c))) {
d = e ;
} else {
fprintf(stderr , "【错误】:数据溢出,c = %I64u\n" , c) ;
f = 0 ;
break ;
}
}
if(f) printf("%I64u\n" , d) ;
}
编译、运行实况:
D:\[exercise]\C>g++ -static -o x x.c
D:\[exercise]\C>x
【错误】:数据溢出,c = 44
D:\[exercise]\C>
结果显示,在采用 64 位整型数的情况下,不要说到 2023,到 44 的时候,数据就已经溢出了。因此,本例不适合用 C / C++ 解题。
我们知道,Python 的整型数没有大小限制,所以,可以考虑用 Python 解决:def common(a , b) :
e = a * b
while b :
a , b = b , a % b
return e // a
d , e = 2 , 2024
for c in range(2 , e) :
d = common(d , c)
print(d)
运行实况:D:\[exercise]\Python>python x.py
1227762956206264118623582541141429549187405177671946464234060731173734645254182863376194152615540505727280653219993317703690524447072610762067793783389005080071796734974865735024986021773726186055458284951748244494933118245578743488914855761878373488868135308841351284532341908809292500010956012208958938141828554364557040594646657783559448232174265632350285186465097411385335942800873708199800202908492724165372047120049528852902328840849252383573180295509547022528099248913962683339793793394550081062639422036767297274851821052696199703915096818519708383217155227462807171855362054148439780401358852626710718548830459614712023730554202373301584134686655430931771332369748911981895426450530517669134338114943553709392259149091137078884995327967573921281316017539167265196705242563435197494576760930221836117819851391468452932981303604385960824026393965398955132764499684480000
D:\[exercise]\Python>
|