U E D R , A S I H C RSS

Summation Of Four Primes/1002

1. Approach

งจ ฒ˜Œ— ฌธ œ ฝณ  Œ€žต —ฐŠตžฅ— ฌธ œ—˜ ฐ €ˆ˜“คด  งŒํ•œ €„“ค„ ณด•˜‹ค. ‹จ †Œˆ˜“ค˜ ํ•ฉด ํ•˜ณ , 4žฆฌ˜ ํ•ฉดณ  ํ•œ‹ค. Œ€žต pseudo code ‹คŒ™€ ฐ™ด ž‘„ํ•ดณด•˜‹ค.

~cpp 
for eachGenNumberFour in GenNumbersFour
  isAllPrimeNumber:
      print eachGenNumberFour
      return
print "impossible"

GenNumbersFour = ํ•ฉด x ธ ˆ˜ กฐํ•ฉฆฌŠคํŠธ

ํ•ฉด x ธ ˆ˜ กฐํ•ฉฆฌŠคํŠธ— Œ€ํ•ด –ด–ปฒŒ ตฌํ• Œ ถฆฌํ•˜˜ค‘, †Œˆ˜ฆฌŠคํŠธ  € งŒ“คณ  †Œˆ˜ฆฌŠคํŠธ—„œ 4ฐœ ณจ„œ ํ•ฉ„ ตฌํ•œ ฒฐ€ n ธ€ „ตํ•˜Š” ฐฉฒ•ด ” ฆฌ ตฌํ˜„ํ•  ˆ˜ žˆฒ ตฌ‚˜Š” ƒฐด “ค—ˆ‹ค. ด— Œ€ํ•ด„œ TDDกœ „ํ–‰.
~cpp 
class PrimeNumberList:
    def __init__(self,aNum):
        self.size = aNum
        self.resultTable = []
        self.createList()

    def createList(self):
        for i in range(2,self.size+1):
            if self.isPrimeNumber(i):
                self.resultTable.append(i)

    def isPrimeNumber(self,aNum):
        for eachValue in self.resultTable:
            if aNum % eachValue == 0:
                return False
        return True

    def getList(self):
        return self.resultTable

class PrimeNumberTest(unittest.TestCase):
    def testPrimeNumber(self):
        self.assertEquals([2,], PrimeNumberList(2).getList())
        self.assertEquals([2,3], PrimeNumberList(3).getList())
        self.assertEquals([2,3,5], PrimeNumberList(5).getList())
        self.assertEquals([2,3,5,7,11], PrimeNumberList(12).getList())
        self.assertEquals([2,3,5,7,11,13,17,19], PrimeNumberList(20).getList())
ทธฆฌณ  †Œˆ˜ฆฌŠคํŠธกœ€„ฐ 4ฐœ ตฌํ•˜Š” ฐฉฒ•— Œ€ํ•ด ƒฐํ•˜‹ค. งจ ฒ˜Œ— Œ€ํ•ด„œŠ” ค‘ณต„ ํ—ˆšฉํ•˜ฉด •ˆ˜Š” „ •Œณ  ตฌํ˜„ํ•˜˜€‹ค. ทธŸฌ‹ค€ ฌธ œ—„œ ค‘ณต„ ํ—ˆšฉํ•œ‹คŠ” ‚ฌ‹ค„ •Œณ  ‹ค‹œ ตฌํ˜„.
~cpp 
def selectionFour(aList):
    sizeOfList = len(aList)
    for i in range(sizeOfList):
        for j in range(sizeOfList):
            for k in range(sizeOfList):
                for l in range(sizeOfList):
                    yield [aList[i],aList[j],aList[k],aList[l]]

2. full source

~cpp 
from __future__ import generators
import psyco

class PrimeNumberList:
    def __init__(self,aNum):
        self.size = aNum
        self.resultTable = []
        self.createList()

    def createList(self):
        for i in range(2,self.size+1):
            if self.isPrimeNumber(i):
                self.resultTable.append(i)

    def isPrimeNumber(self,aNum):
        for eachValue in self.resultTable:
            if aNum % eachValue == 0:
                return False
        return True

    def getList(self):
        return self.resultTable

def selectionFour(aList):
    sizeOfList = len(aList)
    for i in range(sizeOfList):
        for j in range(sizeOfList):
            for k in range(sizeOfList):
                for l in range(sizeOfList):
                    yield [aList[i],aList[j],aList[k],aList[l]]

def main():
    n = int(raw_input('input number : '))
    primeNumberList = PrimeNumberList(n)
    for eachPrimeNumberSeq in selectionFour(primeNumberList.getList()):
        if sum(eachPrimeNumberSeq) == n:
            print eachPrimeNumberSeq
            return
    print "Impossible"

if __name__=="__main__":
    #unittest.main(argv=('','-v'))
    psyco.full()
    main()

3. ฌธ œ  - 4ดˆ ด‚ด— ‹ตด ‚˜˜ค€ •ŠŠ”‹ค.

ŠคํŽ™ƒ 10000000 ฐ’ ‚ด—„œ 4ดˆ ด‚ด— ‹ตด ‚˜™€• ํ•œ‹ค. ด— Œ€ํ•ด„œ ํ˜„žฌ˜ ณ‘ชฉ€ — Œ€ํ•ด profiling „ ํ•ด ณด•˜‹ค.
~cpp 
C:\WINDOWS\system32\cmd.exe /c python SummationOfFourPrimes.py
input number : 10000
[2, 2, 23, 9973]
         21065 function calls in 6.387 CPU seconds

   Random listing order was used

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.075    0.075    1.201    1.201 summationoffourprimes.py:13(createList)
        0    0.000             0.000          profile:0(profiler)
        1    5.103    5.103    6.387    6.387 summationoffourprimes.py:88(main)
        1    0.000    0.000    1.201    1.201 summationoffourprimes.py:8(__init__)
     9999    1.126    0.000    1.126    0.000 summationoffourprimes.py:18(isPrimeNumber)
        1    0.000    0.000    0.000    0.000 summationoffourprimes.py:24(getList)
    11061    0.083    0.000    0.083    0.000 summationoffourprimes.py:79(selectionFour)
        1    0.000    0.000    6.387    6.387 <string>:1(?)
~cpp 
C:\WINDOWS\system32\cmd.exe /c python SummationOfFourPrimes.py
input number : 50000
[2, 2, 3, 49993]
         60269 function calls in 24.926 CPU seconds

   Random listing order was used

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.428    0.428   19.348   19.348 summationoffourprimes.py:13(createList)
        0    0.000             0.000          profile:0(profiler)
        1    5.492    5.492   24.923   24.923 summationoffourprimes.py:88(main)
        1    0.000    0.000   19.348   19.348 summationoffourprimes.py:8(__init__)
    49999   18.920    0.000   18.920    0.000 summationoffourprimes.py:18(isPrimeNumber)
        1    0.000    0.000    0.000    0.000 summationoffourprimes.py:24(getList)
    10265    0.083    0.000    0.083    0.000 summationoffourprimes.py:79(selectionFour)
        1    0.003    0.003   24.926   24.926 <string>:1(?)


Hit any key to close this window...
Œ€€„˜ ‹œ„€ †Œˆ˜ํ…Œดธ”„ ž‘„ํ•˜Š” €„ด œ‹ค. ทธž˜„œ ด €„— Œ€ํ•ด„œ Seminar:PrimeNumber ฐธณ , ตœ ํ™”œ •Œณ ฆฌฆ˜œกœ ˆ˜ •ํ•˜˜€‹ค. ทธฆฌณ  —ญ‹œ psyco ดšฉํ•˜˜€‹ค. ทธ ฒฐ, 10000000 ธฐ€ 10ดˆ. ธฐกด •Œณ ฆฌฆ˜˜ ฒฝšฐ 50000ฒˆ ธฐ€ 24ดˆ ดƒ.
~cpp 
C:\WINDOWS\system32\cmd.exe /c python SummationOfFourPrimes.py
input number : 10000
[2, 2, 23, 9973]
         11067 function calls in 5.417 CPU seconds

   Random listing order was used

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        0    0.000             0.000          profile:0(profiler)
        1    0.000    0.000    0.000    0.000 summationoffourprimes.py:48(getList)
        1    0.000    0.000    5.417    5.417 <string>:1(?)
        1    0.000    0.000    0.013    0.013 summationoffourprimes.py:36(createList)
        1    0.000    0.000    0.013    0.013 summationoffourprimes.py:31(__init__)
    11061    0.094    0.000    0.094    0.000 summationoffourprimes.py:103(selectionFour)
        1    5.309    5.309    5.416    5.416 summationoffourprimes.py:112(main)
        1    0.013    0.013    0.013    0.013 summationoffourprimes.py:8(prime2)


Hit any key to close this window...
~cpp 
C:\WINDOWS\system32\cmd.exe /c python SummationOfFourPrimes.py
input number : 50000
[2, 2, 3, 49993]
         10271 function calls in 7.878 CPU seconds

   Random listing order was used

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        0    0.000             0.000          profile:0(profiler)
        1    0.000    0.000    0.000    0.000 summationoffourprimes.py:48(getList)
        1    0.002    0.002    7.878    7.878 <string>:1(?)
        1    0.002    0.002    0.090    0.090 summationoffourprimes.py:36(createList)
        1    0.000    0.000    0.090    0.090 summationoffourprimes.py:31(__init__)
    10265    0.098    0.000    0.098    0.000 summationoffourprimes.py:103(selectionFour)
        1    7.688    7.688    7.876    7.876 summationoffourprimes.py:112(main)
        1    0.088    0.088    0.088    0.088 summationoffourprimes.py:8(prime2)


Hit any key to close this window...
ตœ ํ™” ดํ›„ 50000ฒˆ ธฐ€œกœŠ” ณ‘ชฉ— Œ€ํ•œ €„ ฅด —†‹ค.

~cpp 
C:\WINDOWS\system32\cmd.exe /c python SummationOfFourPrimes.py
input number : 1000000
[2, 2, 13, 999983]
         470994 function calls in 26.768 CPU seconds

   Random listing order was used

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        0    0.000             0.000          profile:0(profiler)
        1    0.000    0.000    0.000    0.000 summationoffourprimes.py:48(getList)
        1    0.040    0.040   26.768   26.768 <string>:1(?)
        1    0.062    0.062    2.708    2.708 summationoffourprimes.py:36(createList)
        1    0.000    0.000    2.708    2.708 summationoffourprimes.py:31(__init__)
   470988    5.244    0.000    5.244    0.000 summationoffourprimes.py:103(selectionFour)
        1   18.777   18.777   26.728   26.728 summationoffourprimes.py:112(main)
        1    2.646    2.646    2.646    2.646 summationoffourprimes.py:8(prime2)


Hit any key to close this window...
—ฌธฐ„œ selectionFour ˜ ฒฝšฐŠ” percall — ฆฌŠ” ‹œ„€  ‹ค. ํ•˜€งŒ, call ํšŸˆ˜ žฒด€ 470988 ฒˆœกœ งŽ‹ค. ด €„— Œ€ํ•ด„œ ข…˜ inlining „ ํ•˜˜€‹ค.
~cpp 
C:\WINDOWS\system32\cmd.exe /c python SummationOfFourPrimes.py
input number : 1000000
(2, 2, 13, 999983)
         6 function calls in 16.671 CPU seconds

   Random listing order was used

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        0    0.000             0.000          profile:0(profiler)
        1    0.000    0.000    0.000    0.000 summationoffourprimes.py:48(getList)
        1   13.736   13.736   16.540   16.540 summationoffourprimes.py:121(main2)
        1    0.067    0.067    2.804    2.804 summationoffourprimes.py:36(createList)
        1    2.738    2.738    2.738    2.738 summationoffourprimes.py:8(prime2)
        1    0.131    0.131   16.671   16.671 <string>:1(?)
        1    0.000    0.000    2.804    2.804 summationoffourprimes.py:31(__init__)


Hit any key to close this window...
10000000 — Œ€ํ•ด„œŠ” 7.49 ดˆ ธฐก (profiler ดšฉํ•  ฒฝšฐ †„ €ํ•˜€ žˆธฐ •Œฌธ— profiler „ณ  psyco งŒœกœ ‹คํ–‰)


4. ตœ ํ™” 2ฐจ : ฌธ œ ˜—ญ„ „ดธฐ

„ก inlining „ ‹œ€งŒ —ฌ „ํ•œ ฒƒ€ selectionFour €„ด‹ค. selectionFour € ‹คํ–‰˜Š” ฒฝšฐ˜ ตœ•…˜ ฒฝšฐŠ” (n Œ€˜ †Œˆ˜ˆ˜)^4 € œ‹ค. ด € ” smart ํ•˜ฒŒ ํ•  ฐฉฒ•ด —†„Œ.
†Œˆ˜™€ € จํ•˜—ฌ € ” ˜‘˜‘ํ•˜ฒŒ €ƒ‰ํ•  ฐฉฒ•ด กดžฌํ•˜ฆฌ ƒฐํ•œ‹ค.
€žฅ „‹จํ•œ •„ด””–ดกœ, 4ฒˆงธ ฐ’— Œ€ํ•ด„œŠ” ฐ”กœ „‚ฐ„ ํ•  ˆ˜€ žˆฒ ‹ค.

selectionFour ‹คŒ ฐ™ด ˆ˜ •ํ•˜˜€‹ค.
~cpp 
def selectionFour(aList,aNum):
    sizeOfList = len(aList)
    for i in range(sizeOfList):
        for j in range(sizeOfList):
            for k in range(sizeOfList):
                for l in range(sizeOfList):
                    expectedLastValue = aNum-(aList[i]+aList[j]+aList[k])
                    if expectedLastValue in aList:
                        yield [aList[i],aList[j],aList[k],expectedLastValue]
                    else:
                        break
inlining „ ํ•˜€ •Š•˜Œ—„ 6.4ดˆŒ€ ธฐกํ•˜˜€‹ค. inlining „ ํ•˜ฉด 5.7ดˆŒ€ ธฐกํ•œ‹ค.

5. ตœ ํ™” 3ฐจ : PyRex

PyRex Š” Python ฝ”“œ C ฝ”“œกœ  „ํ™˜ํ•ด€‹ค. ด ดšฉ, C ชจ“ˆกœ งŒ“ค–ด ปดํŒŒํ•˜ณ  ฒฐ ‹คํ–‰. ˜™ธกœ 7.x Œ€กœ, PsyCo „•Œณด‹ค ˜คํžˆ ค „Šฅด –จ–ดกŒ‹ค. C ฝ”“œ ณด‹ˆ ›ฌธ.  „ํ˜€ •Œ•„ˆ˜€ —†Š” ฝ”“œ‹ค. ฐจฆฌ —ํ•˜ฒŒ ง ‘ ž‘„ํ•ดŠ”ฒŒ „Šฅํ–ฅƒ ƒœกœŠ” œ ฆฌํ•˜ฒ ‹คŠ” ƒฐ.

6. ํ•œ  Œ€„ Š‚€ 

  • ด „—„ Š‚€  ด€งŒ, ํ•œ€€ ฌธ œ •„ ŠฒŒ ํ’€–ดณด คณ  ํ•˜Š” ฒƒ„ —ฌŸฌ€€กœ ํ•™Šตด œ‹ค.
  •  •— Œ€ํ•œ ธฐก. ฌด–ธ€ ž˜ €ฆฌ€ •ˆ˜Š” ƒํ™ฉด ˜คž˜ •Œ, ํ•œ “ค ‚ฌ‹ค“ค„ ธฐกํ•ดณดŠ” ฒƒ€ ƒ‹ํžˆ „›€ด œ‹ค.

7. ” ํ–ˆ—ˆœฉด ํ•˜Š”  . ณด™„

  • PrimeNumber ˜ ตœ ํ™”— Œ€ํ•ด„œ. ธฐกด— žˆŠ” •Œณ ฆฌฆ˜ด •„‹Œ, ง ‘ ตœ ํ™” ‹œ„ํ•ดณดŠ” ฒƒœกœ ” งŽ€ ฒƒ„ ํ•™Šตํ•  ˆ˜ žˆœฆฌ. ดฒˆ˜ ฒฝšฐŠ” 2‹œ„ ž‘—…œกœ „ํš„ žก•˜˜ €„กœ.
  • ดŸฌํ•œ ฌธ œ˜ ฒฝšฐ ํŠ • •Œณ ฆฌฆ˜˜ •„ ตœ ํ™” œ ฒฐด ‹ตดธฐ ณด‹คŠ”, ฌด–ธ€ ‹คฅธ ฐจ›—„œ ดค„•Œ „ˆฌด‚˜ ฆฌ ‹ตด ‚˜˜คฒŒ ˜Š” ฒฝšฐฒƒด ถ”ธก.  „ํ˜€ ‹คฅธ ฐฉฒ•˜ –ดํ”„กœ˜„ ƒฐํ•ดณดณ  ‹ถ‹ค.

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:28:09
Processing time 0.0332 sec