add: more samples and make them work
This commit is contained in:
44
samples/3np1.rpg
Normal file
44
samples/3np1.rpg
Normal file
@@ -0,0 +1,44 @@
|
||||
**FREE
|
||||
Ctl-Opt Main(ThreeNPlusOne);
|
||||
|
||||
Dcl-Proc ThreeNPlusOne;
|
||||
Dcl-S n Packed(10);
|
||||
Dcl-S counter Int(10) Inz(0);
|
||||
Dcl-S input_prompt VarChar(50) Inz('Enter a positive integer (or 0 to exit):');
|
||||
|
||||
// Use an infinite loop, exit when user enters 0
|
||||
Dow (1 = 1);
|
||||
Dsply input_prompt ' ' n;
|
||||
|
||||
If n = 0;
|
||||
Leave;
|
||||
EndIf;
|
||||
|
||||
If n < 0;
|
||||
input_prompt = 'Positive integers only. Enter a number:';
|
||||
Iter;
|
||||
EndIf;
|
||||
|
||||
// Start sequence calculation
|
||||
input_prompt = 'Enter a positive integer (or 0 to exit):'; // Reset prompt
|
||||
counter = 0;
|
||||
Dsply ('Sequence for ' + %Char(n) + ':');
|
||||
|
||||
Dow n > 1;
|
||||
If %Rem(n:2) = 0;
|
||||
// n is even, divide by 2
|
||||
n = n / 2;
|
||||
Else;
|
||||
// n is odd, multiply by 3 and add 1
|
||||
n = (n * 3) + 1;
|
||||
EndIf;
|
||||
|
||||
counter = counter + 1;
|
||||
Dsply %Char(n);
|
||||
EndDo;
|
||||
|
||||
Dsply ('Reached 1 in ' + %Char(counter) + ' iterations.');
|
||||
Dsply ' '; // Add a blank line for readability
|
||||
EndDo;
|
||||
|
||||
End-Proc ThreeNPlusOne;
|
||||
Reference in New Issue
Block a user