crypto cod

Write a procedure that allows the user to input one positive integer and then outputs the difference of the closest higher and lower primes to itself, that is, primality boundary range value which is frequently used in crypto-coding. What does it mean if an input has small or bigger range value? Explain this at the end of your code with abbrevations.




Is there any help with this question? I have no idea how to solve this problem
Last edited on
1
2
3
primes = [x for x in N if is_prime(x)]
lower_prime = *(lower_bound(primes, n)-1)
upper_prime =*upper_bound(primes, n)

i couldn't write can you throw in full code
Break the code down into smaller goals. A prime number is a number that is only divisible by 1 and itself, meaning that it is not divisible by any number from 1 until itself (hint: for loop). There's other optimizations that can happen, but for now just worry about correctness.

The % operator can check for divisibility.
https://stackoverflow.com/questions/12556946/how-does-the-modulus-operator-work

Write a function called "isPrime" or something like that, that takes in an int and return a bool (true or false) saying whether or not it is a prime.

http://www.cplusplus.com/doc/tutorial/functions/
Last edited on
I still couldn't write xd who want to help (ı now little c++ but ı just begin c and ı don't understand anything
closed account (z05DSL3A)
So, what have you done so far? Post your code and people are more likely to help you with it. It also helps them determine what 'level' of code you maybe able to understand.
I HAVE ONLY QUESTİON İN MY HAND

The question is a real life application which is purely transformed through Cyber Security Engineering literature within the sub-discipline of Crypto-Coding.

There will be 3 steps, ie: 2 looping blocks of primality analysis within your algorithm and a range difference monitoring finally, that is:

1-) Running out the Upper-Prime:
2-) Running out the Lower-Prime:
3-) Monitoring the range,
ie: difference of step 1 and step2

Application 1: Application 2:
Input=25 Input=5
1-) Upper Prime= 29 1-) Upper Prime= 7
2-) Lower Prime= 23 2-) Lower Prime= 3
3-) 29-23=6 3-) 7-3=4
A general raw unoptimised pseudocode schematics:

0-) define n, upper_prime, lower_prime, range;
1-) input the number:n ( must be a positive integer )
2-) struct two main free processing “for”- loops, ie:
for(int i=(n+1); ; i++)
{
for( int j=2; j<=i/2; j++) // primality analysis-Lab01
{ // primality analysis-Lab01
// primality analysis-Lab01 – use break !!
} // primality analysis-Lab01
}
// upper-prime value is obtained !
for(int k=(n-1); ; k--)
{
for( int t=2; t<=k/2; t++) // primality analysis-Lab01
{ // primality analysis-Lab01
// primality analysis-Lab01 – use break !!
} // primality analysis-Lab01
}
// lower-prime value is obtained !
3-) Express & run out the primality boundary range by:
range=upper_prime – lower_prime
> primality analysis-Lab01
then go to whatever code you stole that time.
closed account (z05DSL3A)
This should work...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>

bool is_prime (size_t n);
size_t power (size_t a, size_t n, size_t mod);
bool witness (size_t n, size_t s, size_t d, size_t a);

int main (void)
{
    size_t n, upper_prime, lower_prime, range;

    printf ("Input= ");
    scanf_s ("%zu", &n);

    upper_prime = n;
    while (!is_prime (upper_prime)) ++upper_prime;
    lower_prime = n;
    while (!is_prime (lower_prime)) --lower_prime;

    range = upper_prime - lower_prime;

    printf("1-) Upper Prime: %zu\n", upper_prime);
    printf ("2-) Lower Prime: %zu\n", lower_prime);
    printf ("3-) %zu-%zu=%zu",upper_prime,lower_prime,range);

}

size_t power (size_t a, size_t n, size_t mod)
{ 
    size_t power = a;
    size_t result = 1;

    while (n)
    {
        if (n & 1)
            result = (result * power) % mod;
        power = (power * power) % mod;
        n >>= 1;
    }
    return result;
}

bool witness (size_t n, size_t s,size_t d, size_t a)
{
    size_t x = power (a, d, n);
    size_t y = 0;

    while (s)
    {
        y = (x * x) % n;
        if (y == 1 && x != 1 && x != n - 1)
            return false;
        x = y;
        --s;
    }
    if (y != 1)
        return false;
    return true;
}

bool is_prime (size_t n)
{
    if (((!(n & 1)) && n != 2) || (n < 2) || (n % 3 == 0 && n != 3))
        return false;
    if (n <= 3)
        return true;

    size_t d = n / 2;
    size_t s = 1;
    while (!(d & 1))
    {
        d /= 2;
        ++s;
    }

    if (n < 1373653)
        return witness (n, s, d, 2) && witness (n, s, d, 3);
    if (n < 9080191)
        return witness (n, s, d, 31) && witness (n, s, d, 73);
    if (n < 4759123141)
        return witness (n, s, d, 2) && witness (n, s, d, 7) 
        && witness (n, s, d, 61);
    if (n < 1122004669633)
        return witness (n, s, d, 2) && witness (n, s, d, 13) 
        && witness (n, s, d, 23) && witness (n, s, d, 1662803);
    if (n < 2152302898747)
        return witness (n, s, d, 2) && witness (n, s, d, 3) 
        && witness (n, s, d, 5) && witness (n, s, d, 7) 
        && witness (n, s, d, 11);
    if (n < 3474749660383)
        return witness (n, s, d, 2) && witness (n, s, d, 3) 
        && witness (n, s, d, 5) && witness (n, s, d, 7) 
        && witness (n, s, d, 11) && witness (n, s, d, 13);
    return witness (n, s, d, 2) && witness (n, s, d, 3) 
        && witness (n, s, d, 5) && witness (n, s, d, 7) 
        && witness (n, s, d, 11) && witness (n, s, d, 13) 
        && witness (n, s, d, 17);

}

Edit: you may need to use scanf instead of scanf_s depending on your compiler. Visual Studio has deprecated scanf.
Last edited on
woww thank you bro
Topic archived. No new replies allowed.