Search code examples
cnumbersdoublenumber-theory

C program to find positive integers with beautiful property


I made a C program to search for positive integers that has this kind of property:

enter image description here

So clearly I want the program to at least output the number 262144, but my program does not output this. And also 1 has this property, and my program does output this.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

long double digitspow(long int num){

   char numstr[30];

   sprintf(numstr,"%ld",num);   

   int n=strlen(numstr);
   int digits[n];
   long int total=((int) numstr[0])-48;

   for (int i=1; i<n;i++){
            
            digits[i]=((int) numstr[i])-48;
            total=pow(total,digits[i]);
            
   }
   return sqrt(total);

}

int main()
{

long int num;


for (int i=1;i<20000000;i++){

   num=i;
   if (abs(num - digitspow(num))<0.0000001){
    printf("%ld\n",num);
   }

}

Solution

  • I will answer my own question. The mistake is in the way I compute the exponent, the program was computing, for example if num=262144, this (((((2^6)^2)^1)^4)^4) instead of 2^(6^(2^(1^(4^(4))))). So here is a solution:

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    long double digitspow(long long int num){
    
       char numstr[30];
    
       sprintf(numstr,"%ld",num);   
    
       long int n=strlen(numstr);
       int digits[n];
       long long int total=((int) numstr[n-1])-48;
       
    //computing exponent here:
    //
       for (int i=n-2; i>=0;i--){
                
                digits[i]=((int) numstr[i])-48;
                total=pow(digits[i], total);
                
       }
    //
       return sqrt(total);
    
    }
    
    int main()
    {
    
    long long int num;
    
    
    for (long long int i=1;i<262200;i++){
    
       num=i;
       if (abs(num - digitspow(num))<0.0000001){
        printf("%lld\n",num);
        printf("%lf\n",digitspow(num));
       }
    
    }
    }