Escriba un programa para calcular la suma de todos los números cuadrados de menos de 1.000. Un número cuadrado es el número de AA que tiene una raíz cuadrada del número entero, tal como 1, 4, 9, 16 etc
Program Listing: MJP126
http://smallbasic.com/program/?MJP126
TextWindow.Write(“Enter the value of n : “)
upto = TextWindow.ReadNumber()
TextWindow.WriteLine(” “)
t = Clock.ElapsedMilliseconds
method_first()
time[1] = Clock.ElapsedMilliseconds – t
t = Clock.ElapsedMilliseconds
method_second()
time[2] = Clock.ElapsedMilliseconds – t
t = Clock.ElapsedMilliseconds
method_third()
time[3] = Clock.ElapsedMilliseconds – t
timetaken()
Sub method_first
sum = 0
For n = 1 To upto
sum = sum + Math.Power(n,2)
EndFor
TextWindow.WriteLine(“Sum of first “+upto+” square number “+sum)
TextWindow.WriteLine(“_____________________________________________”)
EndSub
Sub method_second
sum = 0
n = upto
sum = (n*(n+1)*((2*n)+1)/6)
TextWindow.WriteLine(“Sum of first “+upto+” square number “+sum)
TextWindow.WriteLine(“_____________________________________________”)
EndSub
Sub method_third
odd = 1
temp = 0
sum = 0
For n = 1 To upto
temp = temp + odd
sum = sum + temp
odd = odd + 2
EndFor
TextWindow.WriteLine(“Sum of first “+upto+” square number “+sum)
TextWindow.WriteLine(“_____________________________________________”)
EndSub
Sub timetaken
For i = 1 To 3
TextWindow.WriteLine(“Time Taken by method “+i+” = “+time[i])
EndFor
EndSub
Program Listing: MJP126
http://smallbasic.com/program/?MJP126
TextWindow.Write(“Enter the value of n : “)
upto = TextWindow.ReadNumber()
TextWindow.WriteLine(” “)
t = Clock.ElapsedMilliseconds
method_first()
time[1] = Clock.ElapsedMilliseconds – t
t = Clock.ElapsedMilliseconds
method_second()
time[2] = Clock.ElapsedMilliseconds – t
t = Clock.ElapsedMilliseconds
method_third()
time[3] = Clock.ElapsedMilliseconds – t
timetaken()
Sub method_first
sum = 0
For n = 1 To upto
sum = sum + Math.Power(n,2)
EndFor
TextWindow.WriteLine(“Sum of first “+upto+” square number “+sum)
TextWindow.WriteLine(“_____________________________________________”)
EndSub
Sub method_second
sum = 0
n = upto
sum = (n*(n+1)*((2*n)+1)/6)
TextWindow.WriteLine(“Sum of first “+upto+” square number “+sum)
TextWindow.WriteLine(“_____________________________________________”)
EndSub
Sub method_third
odd = 1
temp = 0
sum = 0
For n = 1 To upto
temp = temp + odd
sum = sum + temp
odd = odd + 2
EndFor
TextWindow.WriteLine(“Sum of first “+upto+” square number “+sum)
TextWindow.WriteLine(“_____________________________________________”)
EndSub
Sub timetaken
For i = 1 To 3
TextWindow.WriteLine(“Time Taken by method “+i+” = “+time[i])
EndFor
EndSub
No comments:
Post a Comment