Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
maximum number of divisors - need help
Author Message
SunnY

Posted: Thu Feb 12, 2004 6:06 pm   Post subject: maximum number of divisors - need help

hey i am pretty new to turing i know a little bit about it but we jus got this program to write in our programming class that inputs a positive integer,N, from a user and then the output is be the integer in the range 1 to N which has the greatest number of divisors
for ex.

input of 5 the output should be 4 because from #'s 1 to 5, 4 has the great amount of divisors

i am not askin for someone to write me the code jus give me a start or even a idea on how to write this code cause i have no idea how even start the code thanks in advance

Paul

Posted: Thu Feb 12, 2004 6:13 pm   Post subject: (No subject)

Shouldn't the biggest non prime number, nearest to N have the greatest number of divisors? So like if its 98, check 97, if its prime(which it is), then go to the next, 96, which isnt prime, so its 97? not sure, could run a decreasing for loop from N. Yea, as Cervantes says, you can use mod to find out...
Cervantes

Posted: Thu Feb 12, 2004 6:13 pm   Post subject: (No subject)

you'll need to use mod and for statements
recneps

Posted: Thu Feb 12, 2004 6:15 pm   Post subject: (No subject)

Thats a pretty complicated assignment for just beginning Turing, but it just needs some thought. start out by going through the numbers before that number. like
 code: for i:1..n %then for that number, have a for loop that maybe divides each one by 1-5 for j:o..5 if i mod j =0 then numdiv:=numdiv+1%where numdiv is a counter, kind of. end if end for end for

Then, go through the numbers that gives you using ifs or another loop, and weed out the ones with one, then 2, then 3, and so on, until it stops, then the last numbers eliminated would be your answer. Is that a good enough start?
Cervantes

Posted: Thu Feb 12, 2004 6:17 pm   Post subject: (No subject)

not necessarily paul.
say you input 14.
12 --> 1,2,3,4,6,12 = 6
14 --> 1, 2, 7, 14 = 4
Paul

Posted: Thu Feb 12, 2004 6:19 pm   Post subject: (No subject)

Ah, ok, I see Cervantes. And I was hoping to cheat thru this with an easier way. So do you just start from 1, and go all the way to N, modding and countering?
Cervantes

Posted: Thu Feb 12, 2004 6:26 pm   Post subject: (No subject)

yes that's basically it. Then once you've done that you can sort them and take the first (or last, depending on which way you sort) number from that array. or you could use an if statement within for statements to figure out the number.
Cervantes

Posted: Thu Feb 12, 2004 6:31 pm   Post subject: (No subject)

don't look at the code if you want to solve it by yourself. Its a really good problem, I like it involves lots of logic and a little bit of math

\/
\/
\/
\/
\/
\/

 code: var n : int put "Enter num : " .. get n var divs : array 1 .. n of int %number of divisors that that number has for l : 1 .. n     divs (l) := 0 end for for i : 1 .. n     for d : 1 .. n         if i mod d = 0 then             divs (i) += 1         end if     end for end for for j : 1 .. n     put j, " : ", divs (j) end for var answer : int for i : 1 .. n     for k : 1 .. n         if k not= i then             if divs (k) > divs (i) then                 answer := k             end if         end if     end for end for put "" put "# with most factors : ", answer

SunnY

Posted: Thu Feb 12, 2004 7:11 pm   Post subject: (No subject)

WOW!! thanks a lot everyonee...

i read the first couple of posts and was trying to come up with something but it didnt seem to worka dn i came back and someone posted the answer thanks a lott appreciate it i am gonna try my best acutally understand the coding thanks a lot Cervantes and everyone else for their input appreciate it
SunnY

Posted: Fri Feb 13, 2004 10:27 am   Post subject: (No subject)

hey
the code that Cervantes provided worked fine but today i jus noticed that if u put in like 23 it gives u the wrong output it says 22 but 18 and 20 and 12 have more divisors than 22 i tried to figure out the problem but no luck

any help would be appreciated thanks

also 93 gives u the wrong answer as well and couple of more #'s
Cervantes

Posted: Sat Feb 14, 2004 12:37 pm   Post subject: (No subject)

blast! so it does

Here you go

 code: var n : int put "Enter num : " .. get n var divs : array 1 .. n of int %number of divisors that that number has for l : 1 .. n     divs (l) := 0 end for for i : 1 .. n     for d : 1 .. n         if i mod d = 0 then             divs (i) += 1         end if     end for end for for j : 1 .. n     put j, " : ", divs (j) end for var answer : int := 1 for k : 1 .. n     if divs (k) > answer then         answer := k     end if end for put "" put "# with most factors : ", answer

Again, however, that only outputs one number if there is a tie.
SunnY

Posted: Sat Feb 14, 2004 11:40 pm   Post subject: (No subject)

hey man thanks a lot for trying to fix the code but unfortunately it still isnt right cause now for most #'s it keeps saying the # with most factors is 12 which is not righ like try 93 it says 12

for every # greater than 12 up to 119 it says 12 has the greatest factors and then from 120 to 1000 (which is the largest # i tried) it says 120 is the one with the most factors which is incorrect

thanks a lot for trying to help and writing the code i appreciate and u dont have to fix it up or anything i'll try my best too see if i can fix it but thanks a lot for ur help... u dont have to waste ur time anymore on this...
Cervantes

Posted: Sun Feb 15, 2004 10:39 am   Post subject: (No subject)

man that's a tricky problem. I think this is finally right

 code: View.Set ("graphics:500;2000") var n : int put "Enter num : " .. get n var divs : array 1 .. n of int %number of divisors that that number has for l : 1 .. n     divs (l) := 0 end for for i : 1 .. n     for d : 1 .. n         if i mod d = 0 then             divs (i) += 1         end if     end for end for for j : 1 .. n     put j, " : ", divs (j) end for var answer : int := 1 var biggest := minint for i : 1 .. n     if divs (i) > biggest then         biggest := divs (i)         answer := i     end if end for put "" put "# with most factors : ", answer
SunnY

Posted: Sun Feb 15, 2004 7:54 pm   Post subject: (No subject)

thanks a lot it workss perfectt

thanks again appreciate it!
Andy

Posted: Sun Feb 15, 2004 9:19 pm   Post subject: (No subject)

shouldnt this do the samething???

 code: var number, highest, current, final := 0 get number for i : 1 .. number     for j : 1 .. i         if i mod j = 0 then             current += 1         end if     end for     if current > highest then         highest := current         final := i     end if end for put final
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 15 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: