mersenneforum.org

mersenneforum.org (https://www.mersenneforum.org/index.php)
-   Programming (https://www.mersenneforum.org/forumdisplay.php?f=29)
-   -   pari break() usage (https://www.mersenneforum.org/showthread.php?t=17332)

EdH 2012-10-25 02:10

pari break() usage
 
pari gurus,

Can you tell me why I can't seem to get the break command to break within a loop, instead of executing the rest of the loop prior to breaking?
[code]
findcycle(a,b)=
{
for(e=a,b,
i=e;
for(c=1,10,
i=sigma(i)-i;
if(i<e,
break());
if(i==e,
print(i," ",c);
break()));
print(e))
}
[/code]I want the ifs to break out of the inner loop immediately, but they don't; they continue to the end of the loop invoking the print(e). That means if the first if is true, the second uselessly gets evaluated anyway as well as the print(e) line. If I use break(2), it breaks the outside for loop.

BTW, I have tried next(), with identical results.

I guess along an alternate route, is there an else type command?

Thanks!

Batalov 2012-10-25 02:18

[QUOTE=EdH;315868]I want the ifs to break out of the inner loop immediately, but [I]they don't[/I]; they continue to the end of the loop invoking the print(e).[/QUOTE]
No, [I]they do[/I]. The inner loop is indeed broken when you ordered it _and then_ print(e) is executed. Because it is outside of the inner loop.

Batalov 2012-10-25 02:21

If you indent your own code correctly, you will see it easier:
[CODE]findcycle(a,b)=
{
for(e=a,b,
i=e;
for(c=1,10,
i=sigma(i)-i;
if(i<e,
break());
if(i==e,
print(i," ",c);
break())
);
print(e)
)
}[/CODE]

EdH 2012-10-25 02:52

[QUOTE=Batalov;315871]If you indent your own code correctly, you will see it easier:
[/QUOTE]
Thanks! I was chopping down a much more involved version and didn't do a good job. That will teach me - probably not.

(I'm glad I used my actual code, instead of "constructing" something for the post...)

Again, thanks!

Batalov 2012-10-25 03:04

Oh, I do it myself all the time. When I prototype PE problems in Pari, I frequently type a monstrous ever-growing one-liner (without indentation, obviously). It's a pain to watch later. Here's an example:
[CODE]forprime(p=2,10000,if(p%10==1||p%10==9,d=divisors(p-1);for(j=2,#d,i=d[j]*p;if(i>10^8,break);if(fibonacci(i)%p^2==0,write("presieve1",p" "i);break))))

[/CODE]
(I rewrote it in C, later; Not only rewrote much prettier but without even touching fibonacci(); this was just brainstorming -- Uggggh!)

EdH 2012-10-25 03:40

[QUOTE=Batalov;315876]Oh, I do it myself all the time. When I prototype PE problems in Pari, I frequently type a monstrous ever-growing one-liner (without indentation, obviously). It's a pain to watch later. Here's an example:
[CODE]forprime(p=2,10000,if(p%10==1||p%10==9,d=divisors(p-1);for(j=2,#d,i=d[j]*p;if(i>10^8,break);if(fibonacci(i)%p^2==0,write("presieve1",p" "i);break))))

[/CODE](I rewrote it in C, later; Not only rewrote much prettier but without even touching fibonacci(); this was just brainstorming -- Uggggh!)[/QUOTE]
I used to write stuff like that in a single cell in Excel - I kept bumping up against the character limit. I still have a scoring workbook in use in several places across the country that won't work with greater than 999 competitors due to the cell limitation (unless newer versions expanded that limit). I've been trying to clean my code up lately, what code I still write. Someday, I have to actually comment things. I've run into the problem of not being able to read my own code for lack of comments.

Hmm... I'm rambling... Gotta go.

Thanks again.


All times are UTC. The time now is 10:17.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.