Computer Science Canada maximum number of divisors - need help |
Author: | SunnY [ 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 |
Author: | Paul [ Thu Feb 12, 2004 6:13 pm ] |
Post 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... |
Author: | Cervantes [ Thu Feb 12, 2004 6:13 pm ] |
Post subject: | |
you'll need to use mod and for statements |
Author: | recneps [ Thu Feb 12, 2004 6:15 pm ] | ||
Post 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
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? |
Author: | Cervantes [ Thu Feb 12, 2004 6:17 pm ] |
Post subject: | |
not necessarily paul. say you input 14. 12 --> 1,2,3,4,6,12 = 6 14 --> 1, 2, 7, 14 = 4 |
Author: | Paul [ Thu Feb 12, 2004 6:19 pm ] |
Post 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? |
Author: | Cervantes [ Thu Feb 12, 2004 6:26 pm ] |
Post 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. |
Author: | Cervantes [ Thu Feb 12, 2004 6:31 pm ] | ||
Post 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 \/ \/ \/ \/ \/ \/
|
Author: | SunnY [ Thu Feb 12, 2004 7:11 pm ] |
Post 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 |
Author: | SunnY [ Fri Feb 13, 2004 10:27 am ] |
Post 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 |
Author: | Cervantes [ Sat Feb 14, 2004 12:37 pm ] | ||
Post subject: | |||
blast! so it does Here you go
Again, however, that only outputs one number if there is a tie. |
Author: | SunnY [ Sat Feb 14, 2004 11:40 pm ] |
Post 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... |
Author: | Cervantes [ Sun Feb 15, 2004 10:39 am ] | ||
Post subject: | |||
man that's a tricky problem. I think this is finally right
|
Author: | SunnY [ Sun Feb 15, 2004 7:54 pm ] |
Post subject: | |
thanks a lot it workss perfectt thanks again appreciate it! |
Author: | Andy [ Sun Feb 15, 2004 9:19 pm ] | ||
Post subject: | |||
shouldnt this do the samething???
|