• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

Liczby Carmichaela

Object Storage Arubacloud
0 głosów
136 wizyt
pytanie zadane 16 października 2018 w C i C++ przez BinaryMan Stary wyjadacz (12,620 p.)
edycja 17 października 2018 przez BinaryMan

Witam !
Zmagam się od 2 dni z tym zadaniem. Napisałem 3 wersje tego programu, lecz żadna nie daje określonego rezultatu sad Proszę o nakierowanie mnie co jest nie tak i co mogę w nim zmienić laugh
Zadanie:
 

An important topic nowadays in computer science is cryptography. Some people even think that
cryptography is the only important field in computer science, and that life would not matter at all
without cryptography.
´ Alvaro is one of such persons, and is designing a set of cryptographic procedures for cooking paella.
Some of the cryptographic algorithms he is implementing make use of big prime numbers. However,
checking if a big number is prime is not so easy. An exhaustive approach can require the division of the
number by all the prime numbers smaller or equal than its square root. For big numbers, the amount
of time and storage needed for such operations would certainly ruin the paella.
However, some probabilistic tests exist that offer high confidence at low cost. One of them is the
Fermat test.
Let a be a random number between 2 and n−1 (being n the number whose primality we are testing).
Then, n is probably prime if the following equation holds:
an mod n = a
If a number passes the Fermat test several times then it is prime with a high probability.
Unfortunately, there are bad news. Some numbers that are not prime still pass the Fermat test
with every number smaller than themselves. These numbers are called Carmichael numbers.
In this problem you are asked to write a program to test if a given number is a Carmichael number.
Hopefully, the teams that fulfill the task will one day be able to taste a delicious portion of encrypted
paella. As a side note, we need to mention that, according to Alvaro, the main advantage of encrypted ´
paella over conventional paella is that nobody but you knows what you are eating.
Input
The input will consist of a series of lines, each containing a small positive number n (2 < n < 65000).
A number n = 0 will mark the end of the input, and must not be processed.
Output
For each number in the input, you have to print if it is a Carmichael number or not, as shown in the
sample output.
Sample Input
1729
17
561
1109
431
0
Sample Output
The number 1729 is a Carmichael number.
17 is normal.
The number 561 is a Carmichael number.
1109 is normal.
431 is normal.

CodeEdit#


 

#include <iostream>

using namespace std;
int faktoryzator = 0;


bool faktoryzacja(int n)
{
    int q=n;
    int powtorzenie=0;

    for(int y = 2; y*y<q; y++)
    {
        while(n%y==0)
        {
            faktoryzator++; // counts divisorsq (prime numbers)
            n = n/y;
            //cout<<"y: "<<y<<endl;
            //cout<<"n: "<<n<<endl;
            powtorzenie++;
            //cout<<"Powtorzenia: "<<powtorzenie<<endl;
            if(powtorzenie>1) goto koniec; //eliminate square
            else if(((q-1)%(y-1))!=0)goto koniec; //check condition
        }
        powtorzenie=0;

    }

    //cout<<"Faktoryzator: "<<faktoryzator<<endl;
koniec:
    if(faktoryzator>=3)
    {
        cout<<"The number "<<q<<" is a Carmichael number."<<endl;
        faktoryzator=0;
    }
    else
    {
        cout<<q<<" is normal."<<endl;
        faktoryzator=0;
    }
}

int n=0;

int main()
{
    cin>>n;
    while(n!=0)
    {
        if(n<516)
        {
            cout<<n<<" is normal."<<endl;
        }
        else
        {
            faktoryzacja(n);

        }
        cin>>n;
    }
    return 0;
}

Edit: Teraz mam przekroczono limit czasu.
Dziękuję za pomoc !
 

Zaloguj lub zarejestruj się, aby odpowiedzieć na to pytanie.

Podobne pytania

0 głosów
1 odpowiedź 118 wizyt
pytanie zadane 23 października 2019 w Algorytmy przez Munvik Dyskutant (9,350 p.)
0 głosów
1 odpowiedź 315 wizyt
0 głosów
1 odpowiedź 437 wizyt
pytanie zadane 25 października 2017 w C# przez Stami Gaduła (3,790 p.)

92,576 zapytań

141,426 odpowiedzi

319,652 komentarzy

61,961 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj.

Akademia Sekuraka

Kolejna edycja największej imprezy hakerskiej w Polsce, czyli Mega Sekurak Hacking Party odbędzie się już 20 maja 2024r. Z tej okazji mamy dla Was kod: pasjamshp - jeżeli wpiszecie go w koszyku, to wówczas otrzymacie 40% zniżki na bilet w wersji standard!

Więcej informacji na temat imprezy znajdziecie tutaj. Dziękujemy ekipie Sekuraka za taką fajną zniżkę dla wszystkich Pasjonatów!

Akademia Sekuraka

Niedawno wystartował dodruk tej świetnej, rozchwytywanej książki (około 940 stron). Mamy dla Was kod: pasja (wpiszcie go w koszyku), dzięki któremu otrzymujemy 10% zniżki - dziękujemy zaprzyjaźnionej ekipie Sekuraka za taki bonus dla Pasjonatów! Książka to pierwszy tom z serii o ITsec, który łagodnie wprowadzi w świat bezpieczeństwa IT każdą osobę - warto, polecamy!

...