This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-11-13 14:32:03

agentlee
New Scratcher
Registered: 2012-11-13
Posts: 1

i need help with factors!

like if i want to display a number's factors...how would i go about doing so?

example- n=24 and its factors are 1,2,3,4,6,8,12,24
how would i display those for ANY number?

Offline

 

#2 2012-11-13 16:16:41

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: i need help with factors!

Try using this:

when gf clicked
delete [all v] of [factors v]//Sets up rest of script.
set [number v] to [whatever number you're checking]
set [factor v] to (1)
repeat until <((number) / (2)) < (factor)>
 if <((number) mod (factor)) = (0)>//Checks for factors.
  add (factor) to [factors v]
 end
 change [factor v] by (1)
end
add (number) to [factor v]
Factors should become a list of the number's factors. Now, one method for showing these numbers would be to have the list "factor" show. The other method is to create a sprite with all the numbers 1-10 as well as a comma, and then create a script to stamp itself in such a way that you get a list of numbers. I could give you the script, but it'd take awhile to type up, so I'd rather not.

Anyway, I hope that this helps!

Last edited by ErnieParke (2012-11-13 20:01:11)


http://i46.tinypic.com/35ismmc.png

Offline

 

#3 2012-11-13 19:31:57

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: i need help with factors!

Faster, but not in order from min to max:

when gf clicked
set [number v] to (1036)
delete (all v) of [factors v]
set [f v] to (1)
repeat until<(f) > ([sqrt v] of (number))>
if<((number) mod (f)) = (0)>
add (f) to [factors v]
add ((number)/(f)) to [factors v]
end
change [f v] by (1)
end

Last edited by MoreGamesNow (2012-11-13 19:32:24)


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#4 2012-11-13 20:00:56

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: i need help with factors!

MoreGamesNow wrote:

Faster, but not in order from min to max:

when gf clicked
set [number v] to (1036)
delete (all v) of [factors v]
set [f v] to (1)
repeat until <(f) > ([sqrt v] of (number))>
if<((number) mod (f)) = (0)>
add (f) to [factors v]
add ((number)/(f)) to [factors v]
end
change [f v] by (1)
end

One problem that I can see is that if your number happens to be a perfect square, then you'll get it's square root repeated twice in the list. Other than that, I see no problem with your script.


http://i46.tinypic.com/35ismmc.png

Offline

 

#5 2012-11-13 20:33:18

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: i need help with factors!

ErnieParke wrote:

MoreGamesNow wrote:

Faster, but not in order from min to max:

when gf clicked
set [number v] to (1036)
delete (all v) of [factors v]
set [f v] to (1)
set [root v] to ([sqrt v] of (number))
repeat until <(f) > (root)>
if<((number) mod (f)) = (0)>
add (f) to [factors v]
add ((number)/(f)) to [factors v]
end
change [f v] by (1)
end
if<(round(root)) = (root)>
delete (last) of [factors v]
end

One problem that I can see is that if your number happens to be a perfect square, then you'll get it's square root repeated twice in the list. Other than that, I see no problem with your script.

Ah, nice catch.  Should be fixed


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#6 2012-11-13 23:55:06

BoltBait
Scratcher
Registered: 2009-03-09
Posts: 1000+

Re: i need help with factors!

Here is an old project of mine that may help you: http://scratch.mit.edu/projects/BoltBait/515051


Animated sigs must be banned!
http://boltbait.com/j.pnghttp://boltbait.com/s.pnghttp://boltbait.com/d.pnghttp://boltbait.com/a.pnghttp://boltbait.com/p.png

Offline

 

Board footer