-----------------------------------
SunnY
Thu Feb 12, 2004 6:06 pm
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
Thu Feb 12, 2004 6:13 pm
-----------------------------------
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
Thu Feb 12, 2004 6:13 pm
-----------------------------------
you'll need to use mod and for statements
-----------------------------------
recneps
Thu Feb 12, 2004 6:15 pm
-----------------------------------
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
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
Thu Feb 12, 2004 6:17 pm
-----------------------------------
not necessarily paul.
say you input 14.
12 --> 1,2,3,4,6,12 = 6
14 --> 1, 2, 7, 14 = 4
-----------------------------------
Paul
Thu Feb 12, 2004 6:19 pm
-----------------------------------
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
Thu Feb 12, 2004 6:26 pm
-----------------------------------
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
Thu Feb 12, 2004 6:31 pm
-----------------------------------
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 :)
\/
\/
\/
\/
\/
\/
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
Thu Feb 12, 2004 7:11 pm
-----------------------------------
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
Fri Feb 13, 2004 10:27 am
-----------------------------------
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
Sat Feb 14, 2004 12:37 pm
-----------------------------------
blast! so it does :|
Here you go :P
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
Sat Feb 14, 2004 11:40 pm
-----------------------------------
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
Sun Feb 15, 2004 10:39 am
-----------------------------------
man that's a tricky problem. I think this is finally right :P
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
Sun Feb 15, 2004 7:54 pm
-----------------------------------
thanks a lot it workss perfectt
thanks again appreciate it!
-----------------------------------
Andy
Sun Feb 15, 2004 9:19 pm
-----------------------------------
shouldnt this do the samething???
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