I'm struggling with the following bit of code(/challenge) and I was wondering what would be the best way to solve it.
Pseudo(-like) code
If I understand the code correctly it does:
var val = 1
foreach (char in firstargument):
val = val * ((ascii)char + 27137)
if (val == 92156295871308407838808214521283596197005567493826981266515267734732800)
print "correct"
else
print "incorrect"
Where 'firstargument' is an argument passed to the program like: ./program 123456...
Actual code
#define _GNU_SOURCE
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <gmp.h>
int main(int argc, char *argv[])
{
mpz_t val, mul, cmpval;
char str[513];
int n = 0;
mpz_init(val);
mpz_set_ui(val, 1);
mpz_init(mul);
mpz_init(cmpval);
mpz_set_str(cmpval, "92156295871308407838808214521283596197005567493826981266515267734732800", 10);
if (argc < 2)
{
printf("%s <string>\n", argv[0]);
return -1;
}
strncpy(str, argv[1], 512);
for (n = 0; n < strlen(str); n++)
{
mpz_set_ui(mul, (unsigned long)(str[n] + 27137));
mpz_mul(val, val, mul);
}
if (!(n = mpz_cmp(val, cmpval)))
{
printf("correct.\n");
}
else
{
printf("incorrect.\n");
}
return 0;
}