数学/bignum 挑战需要帮助

发布于 2024-09-16 13:47:17 字数 1251 浏览 14 评论 0原文

我正在努力解决以下代码(/挑战),我想知道解决它的最佳方法是什么。

伪(类似)代码

如果我正确理解代码,它会:

var val = 1
foreach (char in firstargument):
  val = val * ((ascii)char + 27137)

if (val == 92156295871308407838808214521283596197005567493826981266515267734732800)
  print "correct"
else
  print "incorrect"

其中“firstargument”是传递给程序的参数,例如:./program 123456...

实际代码

#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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不必了 2024-09-23 13:47:17

我会从大数字必须能被 ((ascii)theVeryLastChar + 27137) 整除的角度来处理这个问题 - 并尝试找出最后一个字符是什么 - 然后除以它并为“倒数第二个字符”等工作。

I would approach this from the point-of-view that the large number must be divisible by ((ascii)theVeryLastChar + 27137) - and try to figure out what this last char is - and then divide by it and work it for the 'the second from last char' etc.

一袭水袖舞倾城 2024-09-23 13:47:17

这是一个用于计算解决方案的小 Prolog 程序,首先计算具有较低 ASCII 代码的字母。

solve(A) :-
    number_anagram(92156295871308407838808214521283596197005567493826981266515267734732800, L),
    atom_codes(A,L).

number_anagram(N, L) :-
    number_anagram(N, 32, L).

number_anagram(1, 126, []).
number_anagram(N, C, [C|R]) :-
    N > 1,
    F is C + 27137,
    N mod F =:= 0,
    N1 is N / F,
    number_anagram(N1, C, R).
number_anagram(N, C, L) :-
    C < 126,
    C1 is C + 1,
    number_anagram(N, C1, L).

事实证明只有一种解决方案:

$ swipl 

[...]

?- ['number-anagram.pl'].
% number-anagram.pl compiled 0.00 sec, 1,636 bytes
true.

?- solve(A).
A = abbefhiooooorrsy ;
false.

Here's a little Prolog program to compute the solutions, with the letters with lower ASCII codes first.

solve(A) :-
    number_anagram(92156295871308407838808214521283596197005567493826981266515267734732800, L),
    atom_codes(A,L).

number_anagram(N, L) :-
    number_anagram(N, 32, L).

number_anagram(1, 126, []).
number_anagram(N, C, [C|R]) :-
    N > 1,
    F is C + 27137,
    N mod F =:= 0,
    N1 is N / F,
    number_anagram(N1, C, R).
number_anagram(N, C, L) :-
    C < 126,
    C1 is C + 1,
    number_anagram(N, C1, L).

It turns out there is only one solution:

$ swipl 

[...]

?- ['number-anagram.pl'].
% number-anagram.pl compiled 0.00 sec, 1,636 bytes
true.

?- solve(A).
A = abbefhiooooorrsy ;
false.
毁虫ゝ 2024-09-23 13:47:17

我认为这也被称为中国剩余定理/问题。
第欧根尼算法就是解决方案。

I think this is also known as the chinese remainder theorem/problem.
The diogenes algorithm is the solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文