Challenge of the Month - October 2012

General discussion

• 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!

Monday, October 01, 2012 5:32 PM

All replies

• Graphics challenge

While 1 = 1
Program.Delay(5)
Turtle.Speed = 10
Turtle.MoveTo(GraphicsWindow.MouseX,GraphicsWindow.MouseY)
EndWhile

One thing that is impossible is impossible no matter if it is proven so first.

Monday, October 01, 2012 6:48 PM
• Text Challenge:

Import: VMG691-1

EDIT:

VMG691-3 supports up to hundred millions

• Edited by Tuesday, October 02, 2012 10:55 PM
Monday, October 01, 2012 11:18 PM
• 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!'

Tuesday, October 02, 2012 12:45 AM
• Math Challenge 2 - Reverse & Subtract:

```'_______________________________________________________________'
'  Reverse & Subtract   -   Math Challenge 2
'  by GoToLoop (2012/Oct)

'  http://social.msdn.microsoft.com/Forums/en-US/smallbasic
'_______________________________________________________________'
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! (^_^)

Tuesday, October 02, 2012 5:38 AM
• Community Suggested Challenge 1 -by MathMan

the area of a crescent.      LPL617

Tuesday, October 02, 2012 1:33 PM
• My Community Suggested Challenge 1 : RPM049.

Nonki Takahashi

Tuesday, October 02, 2012 4:23 PM
• 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.

Tuesday, October 02, 2012 6:11 PM
• Another 2 excellent answers Nonki and NaochanON.
Tuesday, October 02, 2012 6:12 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```

Tuesday, October 02, 2012 6:23 PM
• 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.

Tuesday, October 02, 2012 10:44 PM
• Community suggested challenge 2: Divisors

Import FJR600

Tuesday, October 02, 2012 10:51 PM
• 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.

While that may be easier, you might end up with a white line on a white background. Mind you, I guess you could run a check for that.... Okay, nevermind :(

'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 Thursday, October 04, 2012 5:01 AM because my comment had no meaning... :(
Thursday, October 04, 2012 5:00 AM
• 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! (^_^)

Wednesday, October 10, 2012 5:57 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

Friday, October 12, 2012 11:58 PM
• This is my Physics Challenge : JDR564.

In this version, I neglected horizontal parameters.

Nonki Takahashi

Saturday, October 13, 2012 5:06 AM
• Nonki, very good, I like the way it has no flicker by using zoom and the spring becoming more transparent as it stretches.
Saturday, October 13, 2012 1:58 PM
• 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-> JDR564-0

Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

Saturday, October 13, 2012 4:17 PM
• Perhaps you could somehow incorporate this code in for the spring's graphics:

`Condensed = 5For i = 1 To GraphicsWindow.Height  Shapes.Remove(rect[i])EndForSize = 50For 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)EndForFor 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])EndForGraphicsWindow.KeyDown = KeyDownGraphicsWindow.KeyUp = KeyUpWhile 1=1  If Up Then    Condensed = Condensed+0.1  EndIf  If Down Then    Condensed = Condensed-0.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)EndWhileSub KeyDown  Key = GraphicsWindow.LastKey  If Key = "Up" Then    Up = "True"  ElseIf Key = "Down" Then    Down = "True"  EndIfEndSubSub KeyUp  Key = GraphicsWindow.LastKey  If Key = "Up" Then    Up = "False"  ElseIf Key = "Down" Then    Down = "False"  EndIfEndSub`

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 Saturday, October 13, 2012 5:29 PM
Saturday, October 13, 2012 5:18 PM
• Here is a simple multi-spring 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.

Saturday, October 13, 2012 5:57 PM
• Math Man

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[i-1])+dy)
EndFor
Shapes.Move(ball,GraphicsWindow.Width/2-25-3,Shapes.GetTop(rect[YN]))
Program.Delay(50)
endfor
EndWhile

Sub GUI
Condensed = 5
Size = 80
YN= math.Ceiling(GraphicsWindow.Height/2)+25
GraphicsWindow.Hide()
For i = 1 To  YN
Shapes.Move(rect[i],x[i],i*2)
Shapes.Zoom(rect[i],z[i],z[i])
EndFor
Shapes.Move(ball,GraphicsWindow.Width/2,Shapes.GetTop(rect[YN]))
Shapes.Move(ball,GraphicsWindow.Width/2-25-3,Shapes.GetTop(rect[YN]))
PM=-1
dy=.2
GraphicsWindow.Show()
EndSub

Sunday, October 14, 2012 8:41 AM
• 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-> JDR564-0

Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

I like fps in your program!  I modified your version and add spring animation.  The program ID is JDR564-2.  This program will not run in Silverlight environment.

Nonki Takahashi

Sunday, October 14, 2012 4:01 PM
• This program will not run in Silverlight environment.

Well, I tried so much to prove it wrong!  @_@

Shoulda worked since JDR564-4 version; but nothing I did would make that spring image to show up!  =(

Gave up at JDR564-6.

Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

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 JDR564-4 version; but nothing I did would make that spring image to show up!  =(

Gave up at JDR564-6.

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 : JDR564-8.  This program works also on Silverlight.

Nonki Takahashi

Sunday, October 14, 2012 11:50 PM
• This is my Physics Challenge : JDR564-9.

In this version, I considered horizontal parameters.

Nonki Takahashi

Monday, October 15, 2012 6:57 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 JDR564-5, 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 white-listed Nonkit.com it's suddenly started working! :D

So JDR564-5 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! (^_^)

Monday, October 15, 2012 7:37 PM
• Very nice spring effect.
Monday, October 15, 2012 10:47 PM
• Thank you GoToLoop and litdev.

I updated it for mouse: JDR564-10.

Nonki Takahashi

Tuesday, October 16, 2012 3:12 AM
• It's so much fun to pull and release the weight!

Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

Tuesday, October 16, 2012 3:28 AM
• Thank you GoToLoop and litdev.

I updated it for mouse: JDR564-10.

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 curios

I changed my password to "incorrect", so whenever I forgot what it was, the computer will remind me: "Password is incorrect"

Tuesday, October 16, 2012 10:36 AM
• Physics Challenge  BWX823-0    http://smallbasic.com/smallbasic.com/program/?BWX823-0

Note: All shapes ! No spring photo.
Tuesday, October 16, 2012 12:44 PM
• Sugoi!!!

Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

Wednesday, October 17, 2012 3:11 AM
• Thanks!  (Doomo!)
Wednesday, October 17, 2012 5:21 AM