数学研发论坛

 找回密码
 欢迎注册

经过改正的计算1至n的欧拉phi函数的程序

热度 1已有 1263 次阅读2015-9-28 15:23 | 程序

PROGRAM EULER_PHI;
TYPE LINKED_PTR=^LINK_NODE;
        LINK_NODE=RECORD
        VALUE:INTEGER;
        NEXT:LINKED_PTR;
END;
VAR N,M,I,R:INTEGER;VAR NODE,G:LINKED_PTR;
PROCEDURE PRINTING(PTR:LINKED_PTR);
VAR P:LINKED_PTR;
BEGIN
P:=PTR;
P:=P^.NEXT;
R:=R+1;
WRITE('  ',R,' ',P^.VALUE);
IF P^.NEXT=NIL THEN EXIT();
PRINTING(P);
FREEMEM(P,SIZEOF(LINKED_PTR));
END;
PROCEDURE SUCCESSOR(VAR PTR:LINKED_PTR);
VAR P:LINKED_PTR;
BEGIN
    IF PTR^.NEXT=NIL THEN BEGIN
        (*CREATE A NEW NODE AND LINK IT TO PREVIOUS ONE*)
        PTR^.NEXT:=ALLOCMEM(SIZEOF(LINK_NODE));
END;
        P:=PTR^.NEXT;PTR:=P;
END;
PROCEDURE ASSIGNMENT(PTR:LINKED_PTR;N:INTEGER);
VAR K:INTEGER;
BEGIN
        FOR K:=2 TO N DO
        BEGIN
                SUCCESSOR(PTR);
        END;
END;
PROCEDURE TRAVERSE_ELEMENTS(O:LINKED_PTR; VAR Q:LINKED_PTR;K:INTEGER);
VAR T:INTEGER;
BEGIN
    FOR T:=2 TO K DO
    BEGIN
        SUCCESSOR(O);
    END;
    Q:=O;
END;
BEGIN
R:=1;
READLN(N);
NODE:=ALLOCMEM(SIZEOF(LINK_NODE));
NODE^.VALUE:=1;
ASSIGNMENT(NODE,N);
M:=2;
WHILE(M<=N) DO
BEGIN
        TRAVERSE_ELEMENTS(NODE,G,M);
        IF G^.VALUE=0 THEN BEGIN
            I:=M;
            WHILE(I<=N) DO
            BEGIN
                TRAVERSE_ELEMENTS(NODE,G,I);
                IF G^.VALUE=0 THEN G^.VALUE:=I;
                G^.VALUE:=(G^.VALUE DIV M)*(M-1);
                I:=I+M;
            END;
            END;
            M:=M+1;
END;
PRINTING(NODE);
READLN();
END.


路过

雷人
1

握手

鲜花

鸡蛋

刚表态过的朋友 (1 人)

发表评论 评论 (2 个评论)

回复 math_humanbeing 2015-9-28 15:25
pascal
回复 happysxyf 2020-2-23 17:20
Pascal,yes!

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 欢迎注册

小黑屋|手机版|数学研发网 ( 苏ICP备07505100号 )

GMT+8, 2020-10-26 09:40 , Processed in 0.038716 second(s), 16 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

返回顶部