SoFunction
Updated on 2025-03-05

C++ Mason Number Source Code Example Analysis

topic:

Requirements: Output all Mason numbers not exceeding 2…n−1 in order from small to large, one per line. If not at all, "None" is output.

Other people's examples

#include <>
int main() {
    int n = 0, m = 0,  e = 0,h=0;
    int i = 0;
    scanf("%d", &n);
    int a = (int)pow(2, n) - 1;//Maximum number    for (i = 2; i < a; i++) {//The numbers are increasing one by one        m = 0;
        for (e = 2; e <= sqrt(i); e++) {//Judge whether it is a prime number            if (i % e == 0) {
                m++;
                break;
            }
        }
        if (m == 0) {
            for (e = 1; e < n; e++) {
                if (2.0 == pow(i + 1, 1.0 / (1.0 * e))) {//Judge whether another condition is met                    printf("%d\n", i);
                    h++;
                    break;
                }
            }
        }
    }
    if (h == 0) {
        printf("None");
    }
    return 0;
}

An error was found: Analysis error: I feel that the idea of ​​finding Mason's number may be wrong.

Whether i in the loop needs to be declared outside the loop.

2. The method to determine whether i is a prime can be changed to using linear sieve method.

3. When judging the Mason number, using the pow function to find the power of 2 will affect the accuracy. It is recommended to use bit operations for optimization.

4. If there is no Mason number at all, "None" should be output outside the loop.

My code

    #include <>
 #include <>
  int main() {
  int n = 0, p = 0, flag = 0;
  scanf("%d", &n);
  for (p = 2; p <n; p++) {
  int i = 2, m = 2; //m is the currently calculated value of 2^p-1  //Linear sieve method to find out whether 2^p-1 is a prime number  while (i <= sqrt(m)) {
  if (m % i == 0) {
  break; //Not prime number, jump out of the loop  }
  i++;
  }
  //If i>sqrt(m) means m is a prime number  if (i > sqrt(m)) {
  printf("%d\n", m);
  flag = 1; //Disable the Mason number of marks  }
  m = (m << 1) + 1; // Calculate the next 2^p-1 value  }
  if (!flag) {
  printf("None");
  }
  return 0;
  }

Show some correctly modified codes

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n;
cin >> n;
bool flag = false; // Record whether there is a Mason numberfor (int i = 2; i < n; i++)
{
    bool isPrime = true; // Record whether i is a prime number    for (int j = 2; j <= sqrt(i); j++)
    {
        if (i % j == 0)
        {
            isPrime = false;
            break;
        }
    }
    if (isPrime) // If i is a prime number    {
        int m = i; // Record the value of i        int sum = 0; // Record the number of digits of i        while (m > 0)
        {
            sum += m % 10;
            m /= 10;
        }
        if (sum == i) // If the sum of the digits of i is equal to i itself, it is a mason number        {
            cout << i << endl;
            flag = true; // There is a mason number        }
    }
}
if (!flag) // If there is no Mason number{
    cout << "None" << endl;
}
return 0;
}

The above is the detailed content of the analysis of the C++ Mason Number source code example. For more information about C++ Mason Number source, please pay attention to my other related articles!