Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5229

Python • Re: Decimal digits extraction of 'Perfect Numbers' and Python's limitations on Raspberry Pi SBCs

$
0
0
'Perfect Number #30'

Code:

import sysdef compute_perfect_number_52():    # Increase the digit limit for integer-to-string conversion    sys.set_int_max_str_digits(10**8)  # Set a sufficiently large limit    # Define the exponent for Perfect Number #30    p = 132049    # Compute the Mersenne prime: 2^p - 1    two_p_minus_1 = (1 << p) - 1    # Compute the perfect number: 2^(p-1) * (2^p - 1)    two_p_minus_2 = (1 << (p - 1))    perfect_number = two_p_minus_2 * two_p_minus_1    # Convert the number to a decimal string    perfect_number_str = str(perfect_number)    # Print the digits in chunks of 1,000    chunk_size = 1000    print(f"Perfect Number #52 has {len(perfect_number_str)} digits.")    for i in range(0, len(perfect_number_str), chunk_size):        print(perfect_number_str[i:i + chunk_size])if __name__ == "__main__":    compute_perfect_number_52()
No delay with 'python3 pn30.py > pn30.txt
-----------------------------------------------------------------------

Perfect Number #39

Code:

import sysdef compute_perfect_number_52():    # Increase the digit limit for integer-to-string conversion    sys.set_int_max_str_digits(10**8)  # Set a sufficiently large limit    # Define the exponent for Perfect Number #39    p =13466917    # Compute the Mersenne prime: 2^p - 1    two_p_minus_1 = (1 << p) - 1    # Compute the perfect number: 2^(p-1) * (2^p - 1)    two_p_minus_2 = (1 << (p - 1))    perfect_number = two_p_minus_2 * two_p_minus_1    # Convert the number to a decimal string    perfect_number_str = str(perfect_number)    # Print the digits in chunks of 1,000    chunk_size = 1000    print(f"Perfect Number #52 has {len(perfect_number_str)} digits.")    for i in range(0, len(perfect_number_str), chunk_size):        print(perfect_number_str[i:i + chunk_size])if __name__ == "__main__":    compute_perfect_number_52()
40 minutes delay for 'python3 pn39.py > pn39.txt'

-----------------------------------------------------
Perfect Number #52

Code:

import sysdef compute_perfect_number_52():    # Increase the digit limit for integer-to-string conversion    sys.set_int_max_str_digits(10**8)  # Set a sufficiently large limit    # Define the exponent for Perfect Number #52    p = 136279841    # Compute the Mersenne prime: 2^p - 1    two_p_minus_1 = (1 << p) - 1    # Compute the perfect number: 2^(p-1) * (2^p - 1)    two_p_minus_2 = (1 << (p - 1))    perfect_number = two_p_minus_2 * two_p_minus_1    # Convert the number to a decimal string    perfect_number_str = str(perfect_number)    # Print the digits in chunks of 1,000    chunk_size = 1000    print(f"Perfect Number #52 has {len(perfect_number_str)} digits.")    for i in range(0, len(perfect_number_str), chunk_size):        print(perfect_number_str[i:i + chunk_size])if __name__ == "__main__":    compute_perfect_number_52()
Unlike a typical 2 minutes delay on x86_64 ,, extreme delay on rPi for 'python3 pn52.py >pn52.txt
The extreme delay on the Raspberry Pi 4 (ARM64) compared to the x86_64 Debian WSL system is likely due to several factors, even though both systems have 8 GB of RAM. Here is a possible cause:

CPU Performance:
Raspberry Pi 4:
Uses an ARM Cortex-A72 CPU, which has a lower clock speed (typically 1.5 GHz) and fewer cores optimized for high-performance workloads.
x86_64 WSL:
Runs on a modern desktop or laptop processor, which is likely much faster per core, has better branch prediction, larger caches, and supports higher clock speeds (e.g., 3+ GHz).
Impact:
Python’s operations for bitwise shifts (1 << p) and large integer multiplications rely heavily on CPU performance. The Pi’s ARM CPU struggles with the massive computation involved in 2136,279,8412^{136,279,841}2136,279,841 operations.
Python's slow BIGINT implementation is explained in

viewtopic.php?p=1459599#p1459599

If you have a week and enjoy a good novel, the thread can be read from the beginning.

Statistics: Posted by ejolson — Sat Nov 23, 2024 8:20 am



Viewing all articles
Browse latest Browse all 5229

Trending Articles