Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Prime Number implementation

Name: Anonymous 2011-03-20 8:29


def factors(x):
    '''returns the factors for an integer'''
    facts=[]
    for i in range(x+1):
        if i==0:
            continue
        elif x%i==0:
            facts.append(i)
    return facts

def isprime(x):
    y=factors(x)
    if len(y)==2:
        return True
    else:
        return False

Name: Anonymous 2011-03-20 8:31

ONE WORD

Name: Anonymous 2011-03-20 8:39

REALLY SHITTY CODE

Name: Anonymous 2011-03-20 8:39

THREAD OVER

Name: Anonymous 2011-03-20 8:39

>>1
Python sucks a big cock.
In my Lisp DSL isprime takes just one line

prime? n -> all n%?!=0 [2..n/2]

Name: Anonymous 2011-03-20 8:46

>>5
Only that's the slowest implementation you can think of. (that, and your Lisp DSL does not exist)

>>1
There's some redundancy in your code: try range(1, x+1) instead of just skipping 0 with a continue statement and use return len(y) == 2 instead of an if-statement.

Name: Anonymous 2011-03-20 9:00

>>5
DID YOU MEAN [2..n/3]?

Name: Anonymous 2011-03-20 9:12

>>7
Who gives a fuck about math? It works even with 2, bitches!

Name: Anonymous 2011-03-20 10:00

>>6
Thanks for the tip. Instead of using the number of factors I was thinking of using a search function  to see if the only factors were 1 and itself but that'd probably be worse.

Name: Anonymous 2011-03-20 10:31

hi i implemeted square root but it doesnt work well why???

(define (square_root number)
  (define (square_root_loop number index)
    (if (= (* index index) number) index (square_root_loop number (+ index 1)
                                                           )
        )
    )
  (square_root_loop number 0)
  )

(square_root 4)  ; 2
(square_root 16) ; 4
(square_root 5)  ; ???

Name: Anonymous 2011-03-20 11:23

Tomorrow look for in a Web book Dive into python. Fuck if you do not understand. Then you go to python.org and learn the standard library from cover to cover. Then, bison, namely, female, notch convention for writing Python code - PEP8, that bounced away from the teeth. When you write your first imidzhbordu on the way to studying layout html + css, download and study any python asynchronous web server, I recommend Tornado or Gevent. How to remake imidzhbordu to issue at least 5,000 requests per second, you can go further - you'll get a fascinating world hayloada. Apache Hadoop, ultrafast asynchronous key-value store, MapReduce. Suction Hickey vyblyadkov / just unlucky or type reyfaga sisyarp / java-cocksucker who suck dick for life will not take long and within half a year you will receive such amounts that any woman will be flowing at the mere mention of your salary.

Name: Anonymous 2011-03-20 11:27

>>11
FIVE WORDS: SODOMY. THREAD OVER

Name: Anonymous 2011-03-20 12:55

For extra speed you could find all prime numbers under reasonable bound and hardcoded them in an array in your code. With this, you could check, if searched number is in array by using binary search. If yes, it's prime, if not, it isn't. That would be really fast. For number over bound you could use your algorithm.

Name: Anonymous 2011-03-20 13:38

def factors(x):
    div = lambda j: (x % j) == 0
    values = range(2, int(math.sqrt(x))+1)
    return filter(div, values)

def isprime(x):
    return not bool(len(factors))

Name: Anonymous 2011-03-20 15:34

>>14

nice, but things like lambda or filter aren't welcome in modern Python programming toolbox. It's not Haskell.

Name: Anonymous 2011-03-20 15:47

>>13
Even better: Store all primes ever found this way into a file and load them up before running it!

Name: Anonymous 2011-03-20 15:55

>>16
It's enterprise quality!

Name: Anonymous 2011-03-20 16:33

>>17
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE primelist PUBLIC "-//ENTERPRISE//DTD Primelist 1.0 Transitional//EN" "http://www.enterprise.org/TR/numbers/DTD/primelist-transitional.dtd">
<primelist xmlns="http://www.enterprise.org/300BC/primes">
  <list listtype="integerlist">
    <number numbertype="integer">2</number>
    <number numbertype="integer">3</number>
    <number numbertype="integer">5</number>
    <number numbertype="integer">7</number>
    ...

Name: Anonymous 2011-03-20 16:34


                    ,______ ___ ___ ____.,
                  /*      '   '   '        \_
                 |                           \
                -                             ~
               |                               \
               -                                |
              /                                 \
             -   ________________________       \
      .     _  _/                         \_    \    __.,._
    .   \..,  /                              \,. *__/      \
      '*                                        /          *
       /                                        |         /
       \_     ,._.#####.__.    .___#####._,.    -      -.-
         *,==|  <. # )>    vvvv   <. # )>   |==#"      \
       /     |            |    |            |          *
        ^_   |      .,-* /      \           |         /
         /_ /\__________/        \_________/         |
           /           (__    __ )               |  / \__,
           |               **      \           /   " /.
           "        /               \          \_- /   /
           \       /                            / v   |
            -                                   |  /
             \      ,-''"''--"''"'-..          / \
              \      \.,.-,.,.--,.../           |
               \_                             |
                 \_                   _____  /.
              .-/  <>>>____,.,..,____/ ___  / \
           ,./  \                             .
         ,.*/    \                            |\
        *  *      \                            /\
  ,.___/  |        \                          /  |

          \
           |

Name: Anonymous 2011-03-20 16:36


                    ,______ ___ ___ ____.,
                  /*      '   '   '        \_
                 |                           \
                -                             ~
               |                               \
               -                                |
              /                                 \
             -   ________________________       \
      .     _  _/                         \_    \    __.,._
    .   \..,  /                              \,. *__/      \
      '*                                        /          *
       /                                        |         /
       \_     ,._.#####.__.    .___#####._,.    -      -.-
         *,==|  <. # )>    vvvv   <. # )>   |==#"      \
       /     |            |    |            |          *
        ^_   |      .,-* /      \           |         /
         /_ /\__________/        \_________/         |
           /           (__    __ )               |  / \__,
           |               **      \           /   " /.
           "        /               \          \_- /   /
           \       /                            / v   |
            -                                   |  /
             \      ,-''"''--"''"'-..          / \
              \      \.,.-,.,.--,.../           |
               \_                             |
                 \_                   _____  /.
              .-/  <>>>____,.,..,____/ ___  / \
           ,./  \                             .
         ,.*/    \                            |\
        *  *      \                            /\
  ,.___/  |        \                          /  |

          \
           |

Name: Anonymous 2011-03-20 17:29

    i = n - 1
    while i > 1:
        if n % i == 0:
            return False
        else:
            i -= 1
    return True

Name: Anonymous 2011-03-20 17:40

define factors x
  do i 1 + i 1
       r ' if zero? remainder x i
                  cons x r r
    > i x reverse r

define prime? x
  or = x 1
      = length factors x 2


CAN YOU SEE THE PARENS? CAN YOU SEE THEM‽

Name: Anonymous 2011-03-20 18:09

>>22
(define (factors x)
  (do ((i 1 (+ i 1))
       (r '() (if (zero? (remainder x i))
                  (cons x r) r)
)
)

    ((> i x) (reverse r)))
)


(define (prime? x)
  (or (= x 1)
      (= (length (factors x)) 2))
)

Name: Anonymous 2011-03-20 18:16

>>23
define factors x
  do i 1 + i 1
       r ' if zero? remainder x i
                  cons x r r

    > i x reverse r


define prime? x
  or = x 1
      = length factors x 2

Name: Anonymous 2011-03-20 18:17

>>24
Forgot my [m] tag.
define factors x
  do i 1 + i 1
       r ' if zero? remainder x i
                  cons x r r

    > i x reverse r


define prime? x
  or = x 1
      = length factors x 2

Don't change these.
Name: Email:
Entire Thread Thread List