Asked by:
Challenge of the Month  October 2012

Welcome to the monthly SmallBasic Challenge!
These challenges are intended for people who are learning to program for the first time or for those returning to programming who want to start using SmallBasic. Some will be easy, some will be hard  but they will all make you think, and more importantly be GREAT FUN!
Please post your solutions / partial solutions / questions / feedback etc. into this thread that will remain 'sticky' for the month. The only rule is that your solution must use standard SmallBasic methods (no extensions).
It would be good if people could post their problems with these challenges so that a discussion can start so that everyone can learn from each other.
Also post feedback on the kind of challenges that you want to see more of in the future.
Community Suggested Challenge 1 by MathMan
Write a program to calculate the area of a crescent.
Community Suggested Challenge2 by Nonki Takahashi
Write a program to find the divisors of a given number.
Physics Challenge
Write a program to model a weight attached to one end of a spring. The other end of the spring is stationary.
Extend to the model if you can to have several springs attached to the weight.
Excuse my crude drawing.
Graphics Challenge
Write a program where the turtle tries to follow the mouse.
For more of a challenge, don't use the Turtle.MoveTo command!
Text Challenge
Write a program to get the user to enter a number and write it out in words, for example 123 is one hundred and twenty three.
Math Challenge 1
Find all the numbers palindromic numbers less than 1000, eg. 1 131, 424 etc (the same forwards as backwards).
Math Challenge 2
Find all the 2 digit numbers that when the digits are reversed and the smaller number subtracted from the larger, the result is 36, for example 15 and 51.
To make it more complex find all the numbers less than 1000, where the difference of the reversed digits is a number entered by the user.
Do you have an idea for a future challenge? Please post it here!
 Edited by litdevModerator Tuesday, October 02, 2012 6:30 PM
General discussion
All replies



Graphics challenge: CMS635
I took Zock77's idea a little further and added colours! The pen colour changes every time the mouse moves!
'A computer without code is like a car without gasoline.' 'Just as gasoline needs a pump to get it into the car, code needs a person to enter it into a computer... That makes me feel important!'

Math Challenge 2  Reverse & Subtract:
'_______________________________________________________________' ' Reverse & Subtract  Math Challenge 2 ' by GoToLoop (2012/Oct) ' http://social.msdn.microsoft.com/Forums/enUS/smallbasic '/thread/a3431e062a46416b855ffcda46b459ec '_______________________________________________________________' LF = Text.GetCharacter(10) TAB = Text.GetCharacter(9) Start: TextWindow.Clear() TextWindow.ForegroundColor = "Green" TextWindow.Write ("Type in wished result on subtractions ") TextWindow.WriteLine("between a number and its mirror:") result = Math.Floor( Math.Abs( TextWindow.ReadNumber() ) ) TextWindow.Write (LF + "Type in maximum number to search ") TextWindow.WriteLine("for mirror subtractions:") limit = Math.Floor( Math.Abs( TextWindow.ReadNumber() ) ) If limit = 0 Then limit = 1000 TextWindow.WriteLine(limit) EndIf TextWindow.ForegroundColor = "Yellow" TextWindow.WriteLine("") FindNumbers() TextWindow.ForegroundColor = "Cyan" TextWindow.WriteLine(LF + "Total numbers found > " + total + LF) TextWindow.ForegroundColor = "Red" TextWindow.Pause() Goto Start '_______________________________________________________________' Sub FindNumbers total = 0 For num = 0 To limit Mirror() If Math.Abs(num  reverse) = result Then total = total + 1 Display() EndIf EndFor EndSub '_______________________________________________________________' Sub Mirror numLen = Text.GetLength(num) reverse = "" For pos = numLen To 1 Step 1 reverse = Text.Append( reverse, Text.GetSubText(num pos,1) ) EndFor EndSub '_______________________________________________________________' Sub Display TextWindow.WriteLine(" " +num+ "  " +reverse+ " " +TAB+ "= " +result) EndSub '_______________________________________________________________'
Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)
 Edited by GoToLoopEditor Tuesday, October 02, 2012 5:45 AM



Good answer GoToLoop, for 36 and 100 as entry how about 04 and 40?
Also note for this case the difference in the digits is always 4 (15 & 51, 26 & 62 etc), can you prove this and this and extending this proof you should be able to show that there are no answers for any difference 2 digit number where the difference number is not a factor 9. So there are only answers for less than 100 if the difference is 0, 9, 18, 27, 36 etc.
 Edited by litdevModerator Tuesday, October 02, 2012 6:19 PM


The Graphics challenge using events  Import Code: RTW654
Turtle.Speed = 10 Timer.Tick = drawTurtle Timer.Interval = 50 sub drawTurtle GraphicsWindow.PenColor = GraphicsWindow.GetRandomColor() Turtle.MoveTo(GraphicsWindow.MouseX,GraphicsWindow.MouseY) EndSub

Graphics challenge: CMS635
I took Zock77's idea a little further and added colours! The pen colour changes every time the mouse moves!
'A computer without code is like a car without gasoline.' 'Just as gasoline needs a pump to get it into the car, code needs a person to enter it into a computer... That makes me feel important!'
This may be a little easier.
Turtle.Speed = 10
While 1 = 1
Program.Delay(5)
GraphicsWindow.PenColor = GraphicsWindow.GetRandomColor()
Turtle.MoveTo(GraphicsWindow.MouseX,GraphicsWindow.MouseY)
EndWhileOne thing that is impossible is impossible no matter if it is proven so first.


Graphics challenge: CMS635
I took Zock77's idea a little further and added colours! The pen colour changes every time the mouse moves!
'A computer without code is like a car without gasoline.' 'Just as gasoline needs a pump to get it into the car, code needs a person to enter it into a computer... That makes me feel important!'
This may be a little easier.
Turtle.Speed = 10
While 1 = 1
Program.Delay(5)
GraphicsWindow.PenColor = GraphicsWindow.GetRandomColor()
Turtle.MoveTo(GraphicsWindow.MouseX,GraphicsWindow.MouseY)
EndWhile
One thing that is impossible is impossible no matter if it is proven so first.
'A computer without code is like a car without gasoline.' 'Just as gasoline needs a pump to get it into the car, code needs a person to enter it into a computer... That makes me feel important!'
 Edited by Joman Mied Thursday, October 04, 2012 5:01 AM because my comment had no meaning... :(

Good answer GoToLoop, for 36 and 100 as entry how about 04 and 40?
Oh, I was already aware my algorithm would fail for such cases as 04  40 = 36!
Problem is, my program asks for a max number limit (defaults to 1000).
So, besides 04, it'd need a palindromic for 004 as well.
And if the user would choose 354987 as limit, then it'd also need 0004, 00004 and 000004. And what about all of the other numbers!Surely I coulda made an algorithm to add up zeros to the left for the mirroring; but due to the way my algorithm works, I've decided to leave my code as simple as possible.
Even though 40 & 04 are palindromic, 4 itself is 1 digit only, then no palindromic for it! :P
Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)
 Edited by GoToLoopEditor Wednesday, October 10, 2012 6:00 PM

This is a sample of Community Suggested Challenge 2 : DCC503
Largest number for this program is 9007199254740992 = 2^53. If you give 2^53+1 then this program shows 2, 4, ... for divisors. For 2^53, it takes about 12 minutes.
Sample Result:
Divisors 0.1
Number? 9007199254740991
1 6361 69431 20394401 441650591 129728784761 1416003655831 9007199254740991
(818.077[s])
Number? 9007199254740992
1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456 536870912 1073741824 2147483648 4294967296 8589934592 17179869184 34359738368 68719476736 137438953472 274877906944 549755813888 1099511627776 2199023255552 4398046511104 8796093022208 17592186044416 35184372088832 70368744177664 140737488355328 281474976710656 562949953421312 1125899906842624 2251799813685248 4503599627370496 9007199254740992
(737.039[s])
Nonki Takahashi



I wish I had the gift for math simulations as you do!
Nevertheless, I couldn't resist to modify your program a little bit, just to use the Timer event for its animation and to include pausing using the space key> JDR5640
Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)
 Edited by GoToLoopEditor Saturday, October 13, 2012 4:18 PM

Perhaps you could somehow incorporate this code in for the spring's graphics:
Condensed = 5
For i = 1 To GraphicsWindow.Height
Shapes.Remove(rect[i])
EndFor
Size = 50
For i = 1 To GraphicsWindow.Height
x[i] = GraphicsWindow.Width/2+Math.Cos(Math.GetRadians(i*Condensed))*Size
z[i] = Math.Max(Math.Sin(Math.GetRadians(i*Condensed))*5,2)
EndFor
For i = 1 To GraphicsWindow.Height
rect[i] = Shapes.AddRectangle(2,2)
Shapes.Move(rect[i],x[i],i)
Shapes.Zoom(rect[i],z[i],z[i])
EndFor
GraphicsWindow.KeyDown = KeyDown
GraphicsWindow.KeyUp = KeyUp
While 1=1
If Up Then
Condensed = Condensed+0.1
EndIf
If Down Then
Condensed = Condensed0.1
EndIf
For i = 1 To GraphicsWindow.Height
x[i] = GraphicsWindow.Width/2+Math.Cos(Math.GetRadians(i*Condensed))*Size
z[i] = Math.Max(Math.Sin(Math.GetRadians(i*Condensed))*5,2)
Shapes.Move(rect[i],x[i],i)
Shapes.Zoom(rect[i],z[i],z[i])
EndFor
Program.Delay(10)
EndWhile
Sub KeyDown
Key = GraphicsWindow.LastKey
If Key = "Up" Then
Up = "True"
ElseIf Key = "Down" Then
Down = "True"
EndIf
EndSub
Sub KeyUp
Key = GraphicsWindow.LastKey
If Key = "Up" Then
Up = "False"
ElseIf Key = "Down" Then
Down = "False"
EndIf
EndSub
I am a 10 year old that loves math, games, and computers. "Everyone is potentially great, you just have to turn it into kinetic greatness."
 Edited by Math Man Saturday, October 13, 2012 5:29 PM

Here is a simple multispring version, import VMB673.
Each spring applies a force in the direction of the spring proportional to how much it is stretched or contracted from its unstretched length (Hooke's Law). The total force is just the sum of the forces from each spring. If we wanted gravity, this is just a constant force down.
here is an extended version with gravity and the anchors/weight can be moved with the mouse, import FDV323.
 Edited by litdevModerator Monday, October 15, 2012 10:49 PM

Math Man
How about this ? (This spring moves endlessly)
GUI()
While "True"
PM=PM*(1)
For K=1 To 20
dy=dy+PM*0.1
For i = 2 To YN
Shapes.Move(rect[i],x[i],Shapes.GetTop(rect[i1])+dy)
EndFor
Shapes.Move(ball,GraphicsWindow.Width/2253,Shapes.GetTop(rect[YN]))
Program.Delay(50)
endfor
EndWhileSub GUI
Condensed = 5
Size = 80
YN= math.Ceiling(GraphicsWindow.Height/2)+25
GraphicsWindow.Hide()
For i = 1 To YN
x[i] = GraphicsWindow.Width/2+Math.sin(Math.GetRadians(i*Condensed))*Size
z[i] = Math.Max(Math.cos(Math.GetRadians(i*Condensed))*5,2)
rect[i] = Shapes.AddRectangle(3,2)
Shapes.Move(rect[i],x[i],i*2)
Shapes.Zoom(rect[i],z[i],z[i])
EndFor
ball= Shapes.AddEllipse(50,50)
Shapes.Move(ball,GraphicsWindow.Width/2,Shapes.GetTop(rect[YN]))
Shapes.Move(ball,GraphicsWindow.Width/2253,Shapes.GetTop(rect[YN]))
PM=1
dy=.2
GraphicsWindow.Show()
EndSub
 Edited by NaochanON Sunday, October 14, 2012 9:08 AM add a photo

I wish I had the gift for math simulations as you do!
Nevertheless, I couldn't resist to modify your program a little bit, just to use the Timer event for its animation and to include pausing using the space key> JDR5640
Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)
Nonki Takahashi
 Edited by Nonki TakahashiModerator Sunday, October 14, 2012 4:17 PM

This program will not run in Silverlight environment.
Well, I tried so much to prove it wrong! @_@
Shoulda worked since JDR5644 version; but nothing I did would make that spring image to show up! =(
Gave up at JDR5646.
Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)
 Edited by GoToLoopEditor Sunday, October 14, 2012 8:52 PM

This program will not run in Silverlight environment.
Well, I tried so much to prove it wrong! @_@
Shoulda worked since JDR5644 version; but nothing I did would make that spring image to show up! =(
Gave up at JDR5646.
Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)
Good job!
I learned about Silverlight Small Basic environment.
 Program.Delay() is needed in loop
 ImageList.GetHeightOfImage() returns zero
 GraphicsWindow.TextInput doesn't work
Then I wrote : JDR5648. This program works also on Silverlight.
Nonki Takahashi
 Edited by Nonki TakahashiModerator Monday, October 15, 2012 12:11 AM


Nice improvement Nonki Takahashi!
I'd also like to clarify some things:
I was already aware that (ImageList.GetHeightOfImage() returns zero). That's why I've patched it w/ > If ld < 1 Then ld = 276
Only barely aware of (GraphicsWindow.TextInput doesn't work). I've fixed the TextInput under JDR5645, replacing it w/ KeyDown. So it shoulda been working.
My problem was that a browser plugin was blocking loading images outside SmallBasic.com domain. So after I've whitelisted Nonkit.com it's suddenly started working! :D
So JDR5645 version was working all along!!!
Now about the (Program.Delay() is needed in loop) thing; the following code lines > Timer.Tick = Animation Timer.Interval = time would guarantee the weight/spring animation wouldn't mash Silverlight plugin that much! It'd be called by a fixed time, just like a Program.Delay(time)! ;P
Well, that's all!
Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)
 Edited by GoToLoopEditor Monday, October 15, 2012 7:45 PM




Thank you GoToLoop and litdev.
I updated it for mouse: JDR56410.
Nonki Takahashi
Is it just messed up through silverlight? If I try it, the springs a barely visible and at wrong position when I pull the weight the springs jump to some other position out of the window on top border. If I try it on my home pc the springs go out of the window at the left side. Kind'a curiosI changed my password to "incorrect", so whenever I forgot what it was, the computer will remind me: "Password is incorrect"

Physics Challenge BWX8230 http://smallbasic.com/smallbasic.com/program/?BWX8230
Note: All shapes ! No spring photo.
 Edited by NaochanON Tuesday, October 16, 2012 12:47 PM add comment

