Message Boards Message Boards

0
|
3680 Views
|
4 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Create a loop with a condition for computation in steps?

Hello Community,

I have a question about how to create a loop made with this code and expressions as follows:

dig = 100;
a = IntegerDigits[1234567890];
b = FromDigits[RandomSample[a]];
c = If[b > 10^9, b, FromDigits[RandomSample[a]]];
d = If[c > 10^9, c, FromDigits[RandomSample[a]]];
e = If[d > 10^9, d, FromDigits[RandomSample[a]]];
f = If[e > 10^9, e, FromDigits[RandomSample[a]]];
g = If[f > 10^9, f, FromDigits[RandomSample[a]]];
h = If[g > 10^9, g, FromDigits[RandomSample[a]]];
m = IntegerPart[h/9999999999*10^dig];

G = FromDigits[RandomSample[IntegerDigits[m]]];
aa = If[G > 10^(dig - 1), G, 
   FromDigits[RandomSample[IntegerDigits[m]]]];
bb = If[aa > 10^(dig - 1), aa, 
   FromDigits[RandomSample[IntegerDigits[m]]]];
cc = If[bb > 10^(dig - 1), bb, 
   FromDigits[RandomSample[IntegerDigits[m]]]];
dd = If[cc > 10^(dig - 1), cc, 
   FromDigits[RandomSample[IntegerDigits[m]]]];
ee = If[dd > 10^(dig - 1), dd, 
   FromDigits[RandomSample[IntegerDigits[m]]]];
ff = If[ee > 10^(dig - 1), ee, 
   FromDigits[RandomSample[IntegerDigits[m]]]];
gg = If[ff > 10^(dig - 1), ff, 
   FromDigits[RandomSample[IntegerDigits[m]]]];
P = If[gg > 10^(dig - 1), gg, 
   FromDigits[RandomSample[IntegerDigits[m]]]];
z = StringPartition[ToString[P], 2];
zz = StringCount[
   z, {"11", "22", "33", "44", "55", "66", "77", "88", "99", "00"}];
s = StringPartition[StringDrop[ToString[P], 1], 2];
ss = StringCount[
   s, {"11", "22", "33", "44", "55", "66", "77", "88", "99", "00"}];
k = N[2*(Count[zz, 1] + Count[ss, 1])/dig, 4]

V = If[k < 0.16, P, "xx"]

The question is this:

When the conditions in line V are satisfied for k the result of P must be shown (only with one result, the first that meets the condition) and when conditions are not satisfied, instead of "xx" I want the program to return to expressions from line G onwards until the condition in V is fulfilled. In a way every time I evaluate this entire code the condition will be fulfilled.

How do I do that? ... I tried everything I know but I do not understand how to do this ... can anyone give me an example that I can apply in this situation or even use my lines of code to solve this doubt of mine?

Thanks a lot for the help.

POSTED BY: Claudio Chaib
4 Replies

You can assign the expressions from line G to a variable with a delayed assignment, and then call While:

proc := (
   G = FromDigits[RandomSample[IntegerDigits[m]]];
   aa = If[G > 10^(dig - 1), G, 
     FromDigits[RandomSample[IntegerDigits[m]]]];
   bb = If[aa > 10^(dig - 1), aa, 
     FromDigits[RandomSample[IntegerDigits[m]]]];
   cc = If[bb > 10^(dig - 1), bb, 
     FromDigits[RandomSample[IntegerDigits[m]]]];
   dd = If[cc > 10^(dig - 1), cc, 
     FromDigits[RandomSample[IntegerDigits[m]]]];
   ee = If[dd > 10^(dig - 1), dd, 
     FromDigits[RandomSample[IntegerDigits[m]]]];
   ff = If[ee > 10^(dig - 1), ee, 
     FromDigits[RandomSample[IntegerDigits[m]]]];
   gg = If[ff > 10^(dig - 1), ff, 
     FromDigits[RandomSample[IntegerDigits[m]]]];
   P = If[gg > 10^(dig - 1), gg, 
     FromDigits[RandomSample[IntegerDigits[m]]]];
   z = StringPartition[ToString[P], 2];
   zz = StringCount[
     z, {"11", "22", "33", "44", "55", "66", "77", "88", "99", 
      "00"}];
   s = StringPartition[StringDrop[ToString[P], 1], 2];
   ss = StringCount[
     s, {"11", "22", "33", "44", "55", "66", "77", "88", "99", 
      "00"}];
   k = N[2*(Count[zz, 1] + Count[ss, 1])/dig, 4]);

proc;
While[k >= 0.16, proc];
V = P
POSTED BY: Gianluca Gorni

You can wrap the expressions from line G onwards in a variable with delayed assignment, and then do a While loop:

procedure := (
   G = FromDigits[RandomSample[IntegerDigits[m]]];
   aa = If[G > 10^(dig - 1), G, 
     FromDigits[RandomSample[IntegerDigits[m]]]];
   bb = If[aa > 10^(dig - 1), aa, 
     FromDigits[RandomSample[IntegerDigits[m]]]];
   cc = If[bb > 10^(dig - 1), bb, 
     FromDigits[RandomSample[IntegerDigits[m]]]];
   dd = If[cc > 10^(dig - 1), cc, 
     FromDigits[RandomSample[IntegerDigits[m]]]];
   ee = If[dd > 10^(dig - 1), dd, 
     FromDigits[RandomSample[IntegerDigits[m]]]];
   ff = If[ee > 10^(dig - 1), ee, 
     FromDigits[RandomSample[IntegerDigits[m]]]];
   gg = If[ff > 10^(dig - 1), ff, 
     FromDigits[RandomSample[IntegerDigits[m]]]];
   P = If[gg > 10^(dig - 1), gg, 
     FromDigits[RandomSample[IntegerDigits[m]]]];
   z = StringPartition[ToString[P], 2];
   zz = StringCount[
     z, {"11", "22", "33", "44", "55", "66", "77", "88", "99", 
      "00"}];
   s = StringPartition[StringDrop[ToString[P], 1], 2];
   ss = StringCount[
     s, {"11", "22", "33", "44", "55", "66", "77", "88", "99", 
      "00"}];
   k = N[2*(Count[zz, 1] + Count[ss, 1])/dig, 4]);

procedure;
While[k >= 0.16, procedure]
V = P
POSTED BY: Gianluca Gorni

Thank you very much for the answer Gianluca... It helped me a lot!

POSTED BY: Claudio Chaib
Posted 5 years ago

Helped me too!

POSTED BY: Jojen Bourgain
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract