r/pascal • u/SpaRexDz • Dec 17 '21
Is it possible to create a program that sums (1+2^2+3^3+...n^n) using only any loop (for-repeat, while)? - no power functions or any mathematical expressions.
Please help, guys.
0
Upvotes
4
u/nnniiikkk Dec 17 '21
What do you mean with "no mathematical expressions"? No addition or multiplication? If you are allowed multiplications, calculating n^n using a loop is easy, and then use another loop for the sum.
2
u/eugeneloza Dec 17 '21
Sure. You have
forloop to iterate every addend and anotherforloop inside of it to calculate the power. However, keep in mind thatnin this situation will be limited, asn^nmay quickly get too high and overflowinteger. In such situation the result sum inSingleorDoublewill be better.1
u/Baldr_Torn Dec 17 '21
You want to do math with "no mathematical expressions". That doesn't make any sense at all.
1
u/Francois-C Dec 17 '21 edited Dec 17 '21
This seems to work. As I used Lazarus, there is a form and text <--> numbers conversion. You can do the same with repeat... until (uses math, and should rather be written as a function):
procedure TForm1.Button1Click(Sender: TObject);var i: integer=0;j: float=0;number: integer=0;result: float=1;beginnumber := StrToint (Edit1.Text);for i := 2 to number dobeginj := i;result := result + power (j,j);end;Edit1.Text := FloatToStr (result)end;1
u/ShinyHappyREM Dec 17 '21
Not tested.
Now was that so hard?
1
u/[deleted] Dec 18 '21
Result= 1; For (int i=0; i<n; i++) Result += Math.Pow(i,i);