Can someone interpret this code for me? I dont understand whats going on. This code is supposed to take a number n that is inputted and show all the numbers below n that are frugal.
A little background, a frugal number is a number where the amount of the digits in the number exceed the amount of digits in its prime factorization (including exponents). So for example, 1024 is a frugal number because 1024 = 2^10, where 1024 has 4 digits and 2^10 has 3 digits.
A code was given to me that compiles and executes exactly what is needed but all the variables, etc have been replaced by random letters, and im trying to figure out what each variable is so that it makes sense to someone reading it. Ive only been able to figure out what FactorDigits, DigitsCount, and n are. The rest i dont know cause i dont really understand what is going on.
#include <stdio.h>
#include <stdlib.h>
int m(int m) {
int c=0;
m: if (!m) goto c;
m/=10;
c++;
goto m;
c: return c;
}
int c(int c) {
int n=2,b=0,DigitsCount=m(c),FactorDigits=0;
m: if (!(c%n)) { b++; c/=n; goto m;}
if (!b) { n++; goto m; }
if (b>1) FactorDigits+=m(b);
FactorDigits+=m(n);
if (c==1 || FactorDigits>=DigitsCount) goto c;
n++;
b=0;
goto m;
c: if (DigitsCount>FactorDigits) return 1;
return 0;
}
int main () {
int n,b=2;
printf("Enter a number: ");
scanf("%d",&n);
m: if (c(b)) printf("%d\n",b);
if (++b<n) goto m;
}
Can someone please help me understand this with as much detail as possible.
Thanks
The function m accepts an int. If the int is anything other than zero, the function returns 0. If m is not zero, it is divided by ten over and over nutil it is zero, and then you get returned the number of times it was divided by ten. This counts the number of digits in the number.
You should just try it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
int m(int m) {
int c=0;
m: if (!m) goto c;
m/=10;
c++;
goto m;
c: return c;
}
int main()
{
for (int i=0;i<10000; i+=99)
{
std::cout << "i = " << i << ", function return = " << m(i) << std::endl;
}
return 0;
}
m could stand for "my function for telling how many digits there are in an int". It's more likely to stand for "the original coder couldn't be bothered coming up with a decent function name".
The original coder threw you a loop by naming two things "c": a variable and a label. And he wasted time in more than one way. He is not your friend.
If you want to know the number of digits in a value, use some math:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <cmath>
int CountCharacters( int value )
{
// Zero always uses exactly one character
if (value == 0) return 1;
// Negative values need an extra character for the minus sign
if (value < 0) return CountCharacters( -value ) + 1;
// (This is the math)
return std::floor( std::log10( value ) ) + 1;
}
Thanks for letting me know so i could stop wasting my time. Unfortunately, my assignment is due tonight and have no chance writing a completely new code by then, since i dont know much if any c. Looks like ill need to find a tutor. Thanks for the help.
Oh, well, think then. If a number is zero, then it takes exactly one digit. Otherwise, you can count the number of digits by using integer division until the number is zero.
For example, to find the number of characters required to print the number 1234: