Computer Science Canada

Functional TYS

Author:  Clayton [ Fri Mar 30, 2007 3:04 pm ]
Post subject:  Functional TYS

Write a program that will list all the factors of a given number. Only one variable may be used maximum. If you are writing this TYS in a procedureal/imperative language, take another crack at it afterwords in something from the functional realm. Good Luck!


Note: Functional Programmers - please don't post functional answers right away, let others post there replies first please, thanks Very Happy

Author:  Catalyst [ Fri Mar 30, 2007 5:11 pm ]
Post subject:  Re: Functional TYS

i love c++ because it lets me write code like this while still having higher level features

c:

        unsigned int n=180;n=n<<16;n++;

        for (;(n&((1<<16)-1))<=((n&(((1<<16)-1)<<16))>>16);n++) {
                if ((((n&(((1<<16)-1)<<16))>>16)%(n&((1<<16)-1)))==0) {
                        cout<<((n&((1<<16)-1)))<<endl; 
                }                            
        }

Author:  Skynet [ Fri Mar 30, 2007 6:35 pm ]
Post subject:  Re: Functional TYS

Lisp:

(defun factor-find-start (N)
  (sort (factor-find N 1) #'<)
)

(defun factor-find (N X)
  (cond
    ( (> X (sqrt N)) nil )
    ( (zerop (mod N X)) 
      (if (eq X (/ N X)) 
       (cons X (factor-find N (1+ X)))
       (cons X (cons (/ N X) (factor-find N (1+ X))))
      )
    )
    ( t (factor-find N (1+ X)) )
  )
)


: