# Project Euler Solutions ### All replies

• To kick off, the First Problem is:

"If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000."

Solution
Id: JLK064
Listing: http://smallbasic.com/program/?JLK064

Tuesday, March 31, 2009 11:35 PM
• I originally posted this as part of a response for the thread on arrays.

Euler Projects 18 and 67 require the use of arrays or other similar data structures. You are given a triangle of numbers, and your job to find the maximum sum traveling from the tip to the base of the triangle. Project 18 has a height of 15, problem 67 has a height of 100.

http://projecteuler.net/index.php?section=problems&id=18
http://projecteuler.net/index.php?section=problems&id=67

The small basic solution can be imported from the ID: XBN618
Wednesday, April 1, 2009 4:43 PM
• Euler Problem #28

http://projecteuler.net/index.php?section=problems&id=28

The goal of this problem is to build a 1000x1000 grid, populate the grid with increasing numbers spiraling out from the center, and find the sum of the diagonals of the resulting grid.

The first solution builds a 1000x1000 grid using the array object, with the needed spiraling values. Once built the code zips through the diagonals to find the sum.

SmallBasic Publish ID: NMQ902

Once I completed the above solution, it occurred to me that I could accomplish the same task by tracking the values of the diagonals as I was building the spiral. The array isn't needed. The second solution solves the problem without the array, and does so in half the time.

Small Basic Publish ID: RBH700

Tuesday, April 21, 2009 7:01 PM
• Euler Problem #30

http://projecteuler.net/index.php?section=problems&id=30

The goal of this problem is to find the sum of all numbers where:
the sum of the fifth power of the numbers digits, equals that number
Example: 4150 = 4^5 + 1^5 + 5^5 + 0^5

This solution is a good example of why I have always been a fan of BASIC. Working with strings can be unpleasant in most languages. Basic makes it easy. I managed to cough up this solution in about five minutes, where it would have taken closer to an hour with Java simply because of the need to struggle with string manipulation (yes, I know you could also do some math to separate the digits).

Small Basic Publish ID: RLB652
Tuesday, May 5, 2009 7:54 PM
• Very nice!  Love the elegance of RLB652.
Tuesday, May 5, 2009 8:41 PM
• Another way to solve the Euler problem #28

This program is much faster than the previously posted one. It solves the problem by viewing the diagonals as the result of four different equations, each starting in the middle. It then adds every value for each of the results in the equations to a variable and when it's done it shows the result.

This is the solution i came up with, the program is very simple and should be easy to understand.

ID: QXH159

Sunday, June 21, 2009 9:26 PM
• Nice. You are right gunnar, your solution is fifteen times faster and it is a more elegant solution than mine.
Monday, June 22, 2009 12:14 AM
• Hi,
on the followed link you can find my humble attempt of solution for problem no 69.

The 'graphical' part makes sense only for small part of the triangle (up to 15-20 rows) but generally works for any (reasonable) size.

As this is one of my first excercises, please forgive me some basic mistakes.

http://smallbasic.com/program/?NXP803

ID: NXP803
Wednesday, June 24, 2009 10:51 AM
• I've posted a solution with no looping... :)

Here's the way the young Gauss would have attacked the problem...

Use the Math.Floor function to find the number of multiples of 3 (n3) and 5 (n5) below the given number respectively.

Add up all the natural numbers from 1 to n3 (simple formula is n3x(n3 + 1) /2) and multiply by 3. (sum3)
Do likewise for n5, only multiply by 5 of course! (sum5)

Then, because the multiples coincide at multiples of 15, use similar programming to find the sum of all the multiples of 15.

The answer is then simply sum3 + sum5, minus the sum of the multiples of 15.

Take a look...

http://smallbasic.com/program/?LWS487
Thursday, August 20, 2009 11:17 PM
• I like your approach.  Modern computers make number crunching easier than thinking.

However, doing it by brute force, noting the 'below input number' ...

total = 0
For i = 1 To input-1
If (math.Remainder(i,3) = 0 or math.Remainder(i,5) = 0) Then
total = total+i
EndIf
EndFor
TextWindow.WriteLine("The long answer is :" + total)

So for input = 15 we have:

3,6,9,12 & 5,10, sum= 45, you have 30

BTW we agree on the answer for 1000 - 233168

Perhaps I am missing something in the original problem.
Friday, August 21, 2009 8:32 PM
• No you didn't miss anything - I did! -  a coding error on my part that only showed up when the target was an exact multiple of 15 - just forgot to do "Input - 1" when calculating the multiples of 15 to be subtracted...

Embarrassing!

Thanks for pointing it out...

Revised code at http://smallbasic.com/program/?KNR835

I love the way smallbasic handles really big numbers - input 1000000000000 into the program above...

Anyone know just how big a number it can cope with?
Sunday, August 23, 2009 8:10 PM
• The following gives up at 2^95 on my PC.

i = 1
x = 2
While("True")
i = i+1
x =2*x
TextWindow.WriteLine(i+" : "+x)
EndWhile

Sunday, August 23, 2009 9:35 PM
• My first post in small basic forum

Problem #6
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

http://smallbasic.com/program/?NXP803-0
Tuesday, September 15, 2009 1:58 PM
• This is what it got to on mine (btw it was done before you could snap your fingers):

95 : 39614081257132168796771975168

Then it said this in an error window:

Value was either too large or too small for a Decimal.

at System.Decimal.FCallMultiply(Decimal& result, Decimal d1, Decimal d2)
at Microsoft.SmallBasic.Library.Primitive.Multiply(Primitive multiplicand)
at Microsoft.SmallBasic.Library.Primitive.op_Multiply(Primitive primitive1, Primitive primitive2)
at _SmallBasicProgram._Main()
Thursday, November 19, 2009 5:21 PM
• which is the same thing it got to on your computer because it went over how many digits you could have
Thursday, November 19, 2009 5:22 PM
• Yet another way to solve Euler problem #28

ID: RNT262

Really, just an excuse to play with SmallBasic - pretty cool.  Brings back some of the fun of programming on a TRS-80 or another machine from that era.

-cd [VC++ MVP] Mark the best replies as answers!
Thursday, December 31, 2009 12:29 AM
• Euler problem #25

The problem asks for the first number in the Fibonacci sequence with 1000 digits. It's not hard to write a program to produce the sequence up to a number of digits, but small basic will not handle numbers with more than 30 digits (it crashed each time I tried, maybe am wrong). The solution is think of was to separate the number, as it grows, into sub-numbers, each one of them with less than 30 digits. You can see my solution here

(My browser crashed if I asked to see the sequence up to the target number. The program doesn't, but I would recommend not to see the sequence in the browser)

http://smallbasic.com/smallbasic.com/program/?jdw047

Wednesday, June 2, 2010 1:51 AM
• You can replace

If see = "yes" or see = "no" Then

else

endif

with

If see<>"yes" or see <> "no" Then

textwindow.writeline("you must say 'yes' or 'no'")

goto repeat

endif

~~AirWaves!!~~
Thursday, December 9, 2010 5:46 AM