Znn3bq.jpeg
²é¿´: 695  |  »Ø¸´: 1

holmescn

½ð³æ (ÕýʽдÊÖ)

[½»Á÷] ³õÊÔOpenMP

ÏÈÉÏ´úÂë
CODE:
Program euler41
    Implicit None
    Integer, Parameter :: N = 9
    Integer, Dimension(N) :: Digits
    Integer :: I, R, X

    ! Digits
    Digits = (/(I, I=N,1,-1)/)

    !$OMP PARALLEL SHARED(Digits) Private(R,I,X)
    !$OMP DO
    Do R = 9, 1, -1
        Do I = 1, Arrangement(N, R)
            X = Permutations(R, I)
            If(IsPrime(X)) Then
                Print *, X
            EndIf
        EndDo
    EndDo
    !$OMP END DO
    !$OMP END PARALLEL

Contains

    Function Arrangement(N, M) Result(R)
        Implicit None
        Integer, intent(in) :: N, M
        Integer :: R
        Integer :: I
        R = 1
        Do I = (N-M+1), N
            R = R * I
        End DO
    EndFunction

    Function IsPrime(N) Result(R)
        Implicit None
        Integer :: N, I
        Logical :: R
        R = .True.
        !$OMP PARALLEL SHARED(N, R) PRIVATE(I)
        !$OMP DO
        DO I = 2, Floor(Sqrt(N*1.0))
            If(Mod(N, I) == 0) Then
                R = .False.
            EndIf
        EndDo
        !$OMP END DO
        !$OMP END PARALLEL
        Return
    EndFunction

    Function Permutations(R, nth) Result(V)
        Implicit None
        Integer, Dimension(N) :: Indices
        Integer, Intent(In) :: R, nth
        Integer :: V
        Integer :: M
        Integer :: Factorial
        Integer :: I, J, K

        Indices = 1
        M = nth - 1
        V = 0
        Factorial = Arrangement(N-1, N-1)

        Do I = 1, R
            J = M / Factorial + 1
            M = Mod(M, Factorial)
            If(N.ne.I) Factorial = Factorial / (N-I)

            ! Find the index
            K = 1
            Do
                If(Indices(K) > 0) J = J - 1
                If(J .eq. 0) Exit
                K = K + 1
            End Do
            Indices(K) = 0

            ! Gen the Number
            V = V*10 + Digits(K)
        EndDo
    EndFunction
End Program euler41

ÌâÄ¿ÊÇEuler¹¤³ÌµÄµÚËÄʮһÌâ. Õâ¸öÌâÏÔÈ»ÊÇ¿ÉÒÔ²¢ÐеÄ. ÒòΪÉú³ÉÒ»¸öÅÅÁÐÊý, ºÍÅжÏÕâ¸öÊýÊDz»ÊÇÖÊÊýÊDz¢ÐеÄ.ͬʱ, ÅжÏÒ»¸öÊýÊDz»ÊÇÖÊÊýÒ²ÊÇ¿ÉÒÔ²¢ÐеÄ, ÒòΪÅжÏij¸öÊýÊDz»ÊǸø¶¨ÊýµÄÒòÊýÕâ¸ö²Ù×÷ÊÇindependentµÄ.

ÕâÑù, ¾ÍÓÐÁËÉÏÃæµÄ´úÂë. µ«²»ÖªµÀÊÇʲôµØ·½Ð´µÄ²»¶Ô, ÓÃgfortran ±àÒëµÄʱºò, ʹÓÃ-openmpÑ¡Ïî, ºÃÏñûÓÐÆðʲô×÷ÓÃ. ÔËÐÐËÙ¶È»¹ÊǺÍûÓÐopenmpµÄÒ»Ñù. ʹÓÃtop²é¿´,Ҳû¿´³ö¶àÏß³ÌÀ´. ʹÓÃifort±àÒë,ÔËÐнá¹û³ö´í. ÕæÊÇÓÐЩÓôÃÆ.

¾ßÌåÇé¿ö»¹ÔÚÑо¿ÖÐ. ´ó¼ÒÒ»ÆðÌÖÂÛÌÖÂÛ°É.
»Ø¸´´ËÂ¥

» ²ÂÄãϲ»¶

ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû

holmescn

½ð³æ (ÕýʽдÊÖ)

¡ï
jjdg(½ð±Ò+1): »¶Ó­ÔÚ±¾°æ¿ªÌâÌÖÂÛ 2011-07-14 20:12:32
1. µ÷ÕûÁËһϳÌÐò. ¼õÉÙÁËÒ»Ð©ÖØ¸´ÔËËã.
2. ͬʱ, °ÑOpenMPµÄdirectiveдµÃ¼òµ¥ÁË.
3. gfotran µÄopenmp ÊÇʹÓà -fopenmp, ¶øintelµÄÊÇ-openmp

½á¹û, gfortran -fopenmp -O3 ÕâÑù±àÒë, »¹ÊÇÐèÒª43s²ÅÄÜÔËÐÐÍê. ¶øifort -openmp -O3Ö»ÐèÒª0.6sÁË.
CODE:
Program euler41
    Implicit None
    Integer, Parameter :: N = 9
    Integer, Dimension(N) :: Digits
    Integer :: I, R, X
    Integer :: Fac

    ! Digits
    Digits = (/(I, I=N,1,-1)/)
    Fac = Arrangement(N-1, N-1)

    Do R = 9, 1, -1
    !$OMP PARALLEL DO
        Do I = 1, Arrangement(N, R)
            X = Permutations(R, I, Fac)
            If(IsPrime(X) .and. X > 97000000) Then
                Print *, X
                Stop
            EndIf
        EndDo
    !$OMP END PARALLEL DO
    EndDo
Contains

    Function Arrangement(N, M) Result(R)
        Implicit None
        Integer, intent(in) :: N, M
        Integer :: R
        Integer :: I
        R = 1
        Do I = (N-M+1), N
            R = R * I
        End DO
    EndFunction

    Function IsPrime(N) Result(R)
        Implicit None
        Integer :: N, I
        Logical :: R
        R = .True.
        !$OMP PARALLEL DO
        DO I = 2, Floor(Sqrt(N*1.0))
            If(Mod(N, I) == 0) Then
                R = .False.
            EndIf
        EndDo
        !$OMP END PARALLEL DO
        Return
    EndFunction

    Function Permutations(R, nth, Fac) Result(V)
        Implicit None
        Integer, Dimension(N) :: Indices
        Integer, Intent(In) :: R, nth
        Integer, Intent(In) :: Fac
        Integer :: Factorial
        Integer :: V
        Integer :: M
        Integer :: I, J, K

        Indices = 1
        M = nth - 1
        V = 0
        Factorial = Fac

        Do I = 1, R
            J = M / Factorial + 1
            M = Mod(M, Factorial)
            If(N.ne.I) Factorial = Factorial / (N-I)

            ! Find the index
            K = 1
            Do
                If(Indices(K) > 0) J = J - 1
                If(J .eq. 0) Exit
                K = K + 1
            End Do
            Indices(K) = 0

            ! Gen the Number
            V = V*10 + Digits(K)
        EndDo
    EndFunction
End Program euler41

2Â¥2011-07-14 16:29:24
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû
Ïà¹Ø°æ¿éÌø×ª ÎÒÒª¶©ÔÄÂ¥Ö÷ holmescn µÄÖ÷Ìâ¸üÐÂ
×î¾ßÈËÆøÈÈÌûÍÆ¼ö [²é¿´È«²¿] ×÷Õß »Ø/¿´ ×îºó·¢±í
[¿¼ÑÐ] µ÷¼Á +4 osbbx 2026-04-02 4/200 2026-04-08 22:52 by may_ÐÂÓî
[¿¼ÑÐ] 070300»¯Ñ§279Çóµ÷¼Á +13 ¹þ¹þ¹þ^_^ 2026-04-08 13/650 2026-04-08 22:29 by belle15
[¿¼ÑÐ] 085501»úеӢ¶þ77×Ü·Ö294Çóµ÷¼Á£¬½ÓÊÜ¿çרҵѧϰ +5 ÊØ·¨¹«ÃñØÁ¼Í 2026-04-08 5/250 2026-04-08 21:19 by bljnqdcc
[¿¼ÑÐ] 324Çóµ÷¼Á +17 ÏëÉÏѧÇóµ÷ 2026-04-03 17/850 2026-04-08 20:04 by ÎÒ¼õ·Ê1
[¿¼ÑÐ] 0703»¯Ñ§µ÷¼Á 348·Ö +14 °¦ÎÒ³¬ÕæÃ»ÕÐÁË 2026-04-06 15/750 2026-04-08 19:16 by ÎÒ¼õ·Ê1
[¿¼ÑÐ] »¯Ñ§0703-Ò»Ö¾Ô¸211-338·ÖÇóµ÷¼Á +10 vants 2026-04-05 11/550 2026-04-08 16:02 by screening
[¿¼ÑÐ] 304Çóµ÷¼Á +16 c297914 2026-04-05 17/850 2026-04-08 13:00 by grayjzr
[¿¼ÑÐ] ²ÄÁϹ¤³ÌרҵÈÕÓïÉúÇóµ÷¼Á +9 111623 2026-04-07 9/450 2026-04-07 23:31 by Ò»Ö»ºÃ¹û×Ó?
[¿¼ÑÐ] 313Çóµ÷¼Á +3 Ê®Áùʰ½ 2026-04-07 3/150 2026-04-07 23:20 by lbsjt
[¿¼ÑÐ] 344Çóµ÷¼Á +11 κ×Óper 2026-04-07 11/550 2026-04-07 23:01 by JourneyLucky
[¿¼ÑÐ] 315Çóµ÷¼Á +3 TUZEIQAQ 2026-04-02 3/150 2026-04-07 17:32 by chenp123
[¿¼ÑÐ] 325 µ÷¼Á +6 QQСϺ 2026-04-07 6/300 2026-04-07 15:17 by Ccclqqq
[¿¼ÑÐ] ÇóÖú +3 ¿¨¿¨¶«88 2026-04-06 4/200 2026-04-06 15:28 by going home
[¿¼ÑÐ] 329Çóµ÷¼Á +17 miaodesi 2026-04-02 20/1000 2026-04-05 18:33 by À¶ÔÆË¼Óê
[¿¼ÑÐ] 085500»úеר˶³õÊÔ288Çóµ÷¼Á +3 GZJguo666- 2026-04-05 3/150 2026-04-05 18:06 by jkddd
[¿¼ÑÐ] µ÷¼ÁÇóÖú +10 Ïë»»ÊÖ»ú²»Ïë½âÊ 2026-04-02 13/650 2026-04-05 09:41 by sam3303
[¿¼ÑÐ] 320Çóµ÷¼Á +3 Ò»ÑùÔ² 2026-04-04 3/150 2026-04-04 22:29 by à£à£à£0119
[¿¼ÑÐ] 266·Ö£¬Ò»Ö¾Ô¸µçÆø¹¤³Ì£¬±¾¿Æ²ÄÁÏ£¬Çó²ÄÁÏרҵµ÷¼Á +9 ÍÛºôºßºôºß 2026-04-02 9/450 2026-04-03 12:05 by 1753564080
[¿¼ÑÐ] 296Çóµ÷¼Á +4 sdhu 2026-04-02 4/200 2026-04-02 21:29 by baoball
[¿¼ÑÐ] һ־Ըɽ¶«´óѧ£¬085600£¬344 +7 κ×Óper 2026-04-02 8/400 2026-04-02 21:12 by °ÙÁéͯ888
ÐÅÏ¢Ìáʾ
ÇëÌî´¦ÀíÒâ¼û