導航:首頁 > 軟體知識 > 如何把演算法做成小程序

如何把演算法做成小程序

發布時間:2024-07-09 09:55:19

❶ 怎樣用Python語言編一個小程序

編寫 Python 小程序的皮冊方法燃握宏主要分為以下幾步:

安裝 Python:在編寫 Python 程序之前,需要在計算機上安裝 Python。Python 官網提供了下載安裝程序皮辯的鏈接,可以根據操作系統版本下載安裝程序。

編寫代碼:可以使用任何文本編輯器編寫 Python 代碼。代碼的具體內容根據程序的需求來決定,可以包括各種 Python 原生語法、內置函數、第三方庫等等。

運行程序:可以使用 Python 解釋器來運行 Python 程序。在終端或命令行界面輸入 python 文件名.py 即可執行程序。

下面是一個簡單的示常式序:

❷ 緙栦竴涓綆鍗曠殑C璇璦灝忕▼搴忋傘傘傘傘傚叧浜嶳SA綆楁硶鐨

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//RSA??????
typedef struct RSA_PARAM_Tag
{
unsigned __int64 p, q; //????,?????????
unsigned __int64 f; //f=(p-1)*(q-1),?????????
unsigned __int64 n, e; //??,n=p*q,gcd(e,f)=1
unsigned __int64 d; //??,e*d=1 (mod f),gcd(n,d)=1
unsigned __int64 s; //??,??2^s<=n????s,?log2(n)
} RSA_PARAM;

//????
const static long g_PrimeTable[]=
{
3,
5,
7,
11,
13,
17,
19,
23,
29,
31,
37,
41,
43,
47,
53,
59,
61,
67,
71,
73,
79,
83,
89,
97
};

const static long g_PrimeCount=sizeof(g_PrimeTable) / sizeof(long);const unsigned __int64 multiplier=12747293821;
const unsigned __int64 adder=1343545677842234541;//????

typedef struct _RandNumber
{
/* */
unsigned __int64 randSeed;/* */
}RandNumber;

static void CreateRandNumber(RandNumber *pRand, unsigned __int64 s);
static unsigned __int64 Random(RandNumber *pRand, unsigned __int64 n);

/* */
void CreateRandNumber(RandNumber *pRand, unsigned __int64 s)
{
if(!s)
{
pRand->randSeed= (unsigned __int64)time(NULL);
}
else
{
pRand->randSeed=s;
}
}

/* */
unsigned __int64 Random(RandNumber *pRand, unsigned __int64 n)
{
pRand->randSeed=multiplier * pRand->randSeed + adder;
return pRand->randSeed % n;
}

static RandNumber g_Rnd;

/*
????,??? x=a*b mod n
*/
unsigned __int64 MulMod(unsigned __int64 a, unsigned __int64 b, unsigned __int64 n)
{
return a * b % n;
}

/*
????,??? x=base^pow mod n
*/
unsigned __int64 PowMod(unsigned __int64 base, unsigned __int64 pow, unsigned __int64 n)
{
unsigned __int64 a=base, b=pow, c=1;
while(b)
{
while(!(b & 1))
{
b>>=1; //a=a * a % n; //?????????64????,?????a*a?a>=2^32????????,??????????64?
a=MulMod(a, a, n);
}

b--; //c=a * c % n; //??????,??64???????32????????????????
c=MulMod(a, c, n);
}
return c;
}

/*
Rabin-Miller????,??????1,????0?
n??????
??:????????????,???????????1/4
*/
long RabinMillerKnl(unsigned __int64 n)
{
unsigned __int64 b, m, j, v, i;
unsigned __int64 TWO = 2;
m=n - 1;
j=0; //0?????m?j,??n-1=m*2^j,??m????,j?????
while(!(m & 1))
{
++j;
m>>=1;
}

//1??????b,2<=b<n-1
b=2 + Random(&g_Rnd, n - 3);

//2???v=b^m mod n
v=PowMod(b, m, n);

//3???v==1,????
if(v == 1)
{
return 1;
}

//4??i=1
i=1;

//5???v=n-1,????
while(v != n - 1)
{
//6???i==l,???,??
if(i == j)
{
return 0;
}

//7?v=v^2 mod n,i=i+1
v=PowMod(v, TWO, n);
++i;

//8????5
}

return 1;
}

/*
Rabin-Miller????,??????loop?
??????1,????0
*/
long RabinMiller(unsigned __int64 n, long loop)
{
long i=0;

//?????????,????
for(i=0; i < g_PrimeCount; i++)
{
if(n % g_PrimeTable[i] == 0)
{
return 0;
}
}

//????Rabin-Miller??loop?,??????????????(1/4)^loop
for(i=0; i < loop; i++)
{
if(!RabinMillerKnl(n))
{
return 0;
}
}

return 1;
}

/*
??????bits?(????)???,??32?
*/
unsigned __int64 RandomPrime(char bits)
{
unsigned __int64 base;
do
{
base= (unsigned long)1 << (bits - 1); //??????1
base+=Random(&g_Rnd, base); //????????
base|=1; //??????1,??????
} while(!RabinMiller(base, 30)); //????-????30?
return base; //?????????
}

/*
???????????
*/
unsigned __int64 EuclidGcd(unsigned __int64 p, unsigned __int64 q)
{
unsigned __int64 a=p > q ? p : q;
unsigned __int64 b=p < q ? p : q;
unsigned __int64 t;
if(p == q)
{
return p; //????,?????????
}
else
{
while(b) //?????,gcd(a,b)=gcd(b,a-qb)
{
a=a % b;
t=a;
a=b;
b=t;
}

return a;
}
}

/*
Stein???????
*/
unsigned __int64 SteinGcd(unsigned __int64 p, unsigned __int64 q)
{
unsigned __int64 a=p > q ? p : q;
unsigned __int64 b=p < q ? p : q;
unsigned __int64 t, r=1;
if(p == q)
{
return p; //????,?????????
}
else
{
while((!(a & 1)) && (!(b & 1)))
{
r<<=1; //a?b?????,gcd(a,b)=2*gcd(a/2,b/2)
a>>=1;
b>>=1;
}

if(!(a & 1))
{
t=a; //??a???,??a,b
a=b;
b=t;
}

do
{
while(!(b & 1))
{
b>>=1; //b???,a????,gcd(b,a)=gcd(b/2,a)
} if(b < a)
{
t=a; //??b??a,??a,b
a=b;
b=t;
} b=(b - a) >> 1; //b?a????,gcd(b,a)=gcd((b-a)/2,a)
} while(b);

return r * a;
}
}

/*
??a?b,?x,??a*x =1 (mod b)
?????a*x-b*y=1??????
*/
unsigned __int64 Euclid(unsigned __int64 a, unsigned __int64 b)
{
unsigned __int64 m, e, i, j, x, y;
long xx, yy;
m=b;
e=a;
x=0;
y=1;
xx=1;
yy=1;
while(e)
{
i=m / e;
j=m % e;
m=e;
e=j;
j=y;
y*=i;
if(xx == yy)
{
if(x > y)
{
y=x - y;
}
else
{
y-=x;
yy=0;
}
}
else
{
y+=x;
xx=1 - xx;
yy=1 - yy;
} x=j;
} if(xx == 0)
{
x=b - x;
} return x;
}

/*
??????RSA????
*/
RSA_PARAM RsaGetParam(RandNumber Rnd)
{
RSA_PARAM Rsa={ 0 };
unsigned __int64 t;
Rsa.p=RandomPrime(16); //????????
Rsa.q=RandomPrime(16);
Rsa.n=Rsa.p * Rsa.q;
Rsa.f=(Rsa.p - 1) * (Rsa.q - 1);
do
{
Rsa.e=Random(&Rnd, 65536); //??2^16,65536=2^16
Rsa.e|=1; //??????1,??????,?f?????,???,?????
} while(SteinGcd(Rsa.e, Rsa.f) != 1);

Rsa.d=Euclid(Rsa.e, Rsa.f);
Rsa.s=0;
t=Rsa.n >> 1;
while(t)
{
Rsa.s++; //s=log2(n)
t>>=1;
}

return Rsa;
}

/*
??-????
*/
void TestRM(void)
{
unsigned long k=0;
unsigned __int64 i = 0;

printf(" - Rabin-Miller prime check.\n\n");

for(i=4197900001; i < 4198000000; i+=2)
{
if(RabinMiller(i, 30))
{
k++;
printf("%ul\n", i);
}
}

printf("Total: %ul\n", k);
}

void Usage(void)
{
printf("0. exit\n");
printf("1. encryption\n");
printf("2. decrypt\n");
printf("enter your choice:");
}

int GetChoice(void)
{
char s[80] = {0};
fgets(s, sizeof(s), stdin);

if ('0' > s[0] || '9' < s[0] || '\n' != s[1])
{
return -1;
}
else
{
return s[0] - '0';
}
}

#define ENCRYPT_CODE 1
#define DECRYPT_CODE 2
#define EXIT_CODE 0

#define MAX_SIZE 1024

int main(void)
{
RSA_PARAM r;
char pSrc[MAX_SIZE]={0};
unsigned long n = 0;
unsigned char *q = NULL;
unsigned char pDec[MAX_SIZE] = {0};
unsigned __int64 pEnc[MAX_SIZE] = {0};
unsigned __int64 tmp = 0;
unsigned long i=0;
int choice = 0;

unsigned __int64 s = 0;
CreateRandNumber(&g_Rnd, s);

r=RsaGetParam(g_Rnd);

/*
printf("p=%ul\n", r.p);
printf("q=%ul\n", r.q);

printf("f=(p-1)*(q-1)=%ul\n", r.f);
printf("n=p*q=%ul\n", r.n);
printf("e=%ul\n", r.e);
printf("d=%ul\n", r.d);
printf("s=%ul\n", r.s);
*/

while (1)
{
memset(pSrc, 0, MAX_SIZE);
memset(pDec, 0, MAX_SIZE);
memset(pEnc, 0, MAX_SIZE);

Usage();

choice = GetChoice();
switch(choice)
{
case ENCRYPT_CODE:
printf("Source:%s", pSrc);
scanf("%s", pSrc);
n = strlen(pSrc);
getchar();

q= (unsigned char *)pSrc;
printf("Encode:");

for(i = 0; i < n; i++)
{
tmp = q[i];
pEnc[i]=PowMod(tmp, r.e, r.n);
printf("%x ", pEnc[i]);
}
printf("\n");
break;

case DECRYPT_CODE:
printf("Enter length of ciphertext:");
scanf("%d", &n);
getchar();

for(i=0; i < n; i++)
{
printf("Enter ciphertext(%d):", i+1);
scanf("%x", &pEnc[i]);
getchar();
}

printf("Decode:");
for(i=0; i < n; i++)
{
pDec[i]=PowMod(pEnc[i], r.d, r.n);
printf("%x ", (unsigned long)pDec[i]);
}
printf("\n");
printf("%s\n", (char *)pDec);
break;

case EXIT_CODE:
break;
default:
break;
}

if (EXIT_CODE == choice)
{
break;
}
else
{
continue;
}
}

return 0;
}

閱讀全文

與如何把演算法做成小程序相關的資料

熱點內容
廣州科技職業技術院怎麼樣 瀏覽:167
信息學競賽考什麼小學 瀏覽:282
mysql怎麼切換資料庫 瀏覽:155
支付寶小程序怎麼開啟許可權 瀏覽:292
如何進入信息社會 瀏覽:395
誅仙端游怎麼交易 瀏覽:669
貴溪到南昌鋼材市場坐什麼車 瀏覽:442
做點什麼微商代理最賺錢 瀏覽:355
遙控技術原理屬於什麼學科 瀏覽:544
車子違章信息哪裡查 瀏覽:2
為什麼網店代理要交預存款 瀏覽:615
有哪些發布兼職信息的公眾號 瀏覽:866
市場上泡水車有多少 瀏覽:653
如何按順序對比兩欄的數據 瀏覽:721
二手房交易糾紛如何申請仲裁 瀏覽:61
如何查詢自己的股東信息 瀏覽:66
如何更新股票信息 瀏覽:147
營口招聘市場前景如何 瀏覽:310
升學e網通怎麼填寫個人信息 瀏覽:447
打工仔可以學習什麼技術 瀏覽:261