Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Functional TYS
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Clayton




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
Catalyst




PostPosted: 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; 
                }                            
        }
Skynet




PostPosted: 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)) )
  )
)
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: