# Nombres premiers en Perl



## molgow (9 Décembre 2004)

```
perl -le '$_ = 1; (1 x $_) !~ /^(11+)\1+$/ && print while $_++'
```
 
  Un coup de boule pour celui qui arrive à faire plus court et plus incompréhensible que ça !


----------



## Einbert (10 Décembre 2004)

molgow a dit:
			
		

> ```
> perl -le '$_ = 1; (1 x $_) !~ /^(11+)\1+$/ && print while $_++'
> ```
> 
> Un coup de boule pour celui qui arrive à faire plus court et plus incompréhensible que ça !



En Perl ou en général ? En as, cela doit être bien plus imcompréhensible, mais en ce qui concerne plus court, c'est vrai que c'est difficile   

++


----------



## molgow (11 Décembre 2004)

Peu importe, en fait j'ai trouvé ça sur une page qui parlait du langage APL, un très vieux langage dont je n'avais jamais entendu parler.

 Un programme pour trouver les nombres premiers en APL :






 Parait que c'est un langage pour mathématiciens


----------



## SuperCed (13 Décembre 2004)

Einbert a dit:
			
		

> En Perl ou en général ? En as, cela doit être bien plus imcompréhensible, mais en ce qui concerne plus court, c'est vrai que c'est difficile
> 
> ++



En effet :


```
.section __TEXT,__text,regular,pure_instructions
	.align 2
Ltext0:
	.stabs	"gcc2_compiled.",60,0,0,0
.data
.cstring
	.align 2
LC0:
	.ascii "Nombres premiers :\12\0"
	.align 2
LC1:
	.ascii "temps : %d\12\0"
.section __TEXT,__text,regular,pure_instructions
	.align 2
	.align 2
	.globl _main
.section __TEXT,__text,regular,pure_instructions
	.align 2
_main:
	.stabd	68,0,8
	nop
	nop
	nop
	nop
	mflr r0
	stmw r30,-8(r1)
	stw r0,8(r1)
	lis r0,0xfff3
	ori r0,r0,51824
	stwux r1,r1,r0
	mr r30,r1
	bcl 20,31,"L00000000001$pb"
"L00000000001$pb":
	mflr r31
	addis r2,r30,0xc
	stw r3,13736(r2)
	addis r2,r30,0xc
	stw r4,13740(r2)
	.stabd	68,0,13
LBB2:
	addis r3,r31,ha16(LC0-"L00000000001$pb")
	la r3,lo16(LC0-"L00000000001$pb")(r3)
	bl L_printf$stub
	.stabd	68,0,14
	li r2,0
	li r3,17
	stw r2,72(r30)
	stw r3,76(r30)
	.stabd	68,0,15
	li r2,0
	li r3,0
	stw r2,64(r30)
	stw r3,68(r30)
	.stabd	68,0,16
	addi r0,r30,100
	mr r3,r0
	bl L_time$stub
	.stabd	68,0,17
L6:
	lwz r0,64(r30)
	cmplwi cr7,r0,0
	bgt cr7,L7
	lwz r0,64(r30)
	cmpwi cr7,r0,0
	bne cr7,L8
	lwz r0,68(r30)
	lis r2,0x1
	ori r2,r2,34463
	cmplw cr7,r0,r2
	bgt cr7,L7
L8:
	.stabd	68,0,18
	li r0,1
	stb r0,96(r30)
	.stabd	68,0,19
	lwz r3,72(r30)
	lwz r4,76(r30)
	li r5,0
	li r6,0
	bl L___cmpdi2$stub
	mr r0,r3
	cmpwi cr7,r0,1
	blt cr7,L11
	lwz r3,72(r30)
	lwz r4,76(r30)
	bl L___floatdidf$stub
	addis r2,r30,0xc
	stfd f1,13688(r2)
	b L10
L11:
	lwz r0,72(r30)
	lwz r2,76(r30)
	andi. r11,r0,0
	rlwinm r12,r2,0,31,31
	lwz r0,72(r30)
	slwi r8,r0,31
	lwz r0,76(r30)
	srwi r0,r0,1
	lwz r2,72(r30)
	or r10,r8,r0
	srwi r9,r2,1
	or r2,r11,r9
	or r3,r12,r10
	mr r4,r3
	mr r3,r2
	bl L___floatdidf$stub
	addis r2,r30,0xc
	stfd f1,13688(r2)
	addis r2,r30,0xc
	lfd f0,13688(r2)
	addis r2,r30,0xc
	lfd f0,13688(r2)
	fadd f0,f0,f0
	addis r2,r30,0xc
	stfd f0,13688(r2)
L10:
	addis r2,r30,0xc
	lfd f1,13688(r2)
	bl L_sqrt$stub
	fmr f0,f1
	addis r2,r30,0xc
	addi r2,r2,13680
	fctiwz f0,f0
	stfd f0,0(r2)
	lwz r0,4(r2)
	stw r0,84(r30)
	srawi r0,r0,31
	stw r0,80(r30)
	.stabd	68,0,20
	lwz r2,80(r30)
	lwz r3,84(r30)
	addic r3,r3,1
	addze r2,r2
	stw r2,80(r30)
	stw r3,84(r30)
	.stabd	68,0,21
	li r2,0
	li r3,2
	stw r2,88(r30)
	stw r3,92(r30)
	.stabd	68,0,22
L12:
	lbz r0,96(r30)
	extsb r0,r0
	cmpwi cr7,r0,0
	beq cr7,L13
	lwz r0,80(r30)
	lwz r2,88(r30)
	cmplw cr7,r0,r2
	bgt cr7,L14
	lwz r0,80(r30)
	lwz r2,88(r30)
	cmpw cr7,r0,r2
	bne cr7,L13
	lwz r0,84(r30)
	lwz r2,92(r30)
	cmplw cr7,r0,r2
	bgt cr7,L14
	b L13
L14:
	.stabd	68,0,23
	lwz r2,72(r30)
	lwz r3,76(r30)
	mr r4,r3
	mr r3,r2
	lwz r5,88(r30)
	lwz r6,92(r30)
	bl L___umoddi3$stub
	mr r2,r3
	mr r3,r4
	mr r0,r2
	or r0,r0,r3
	cmpwi cr7,r0,0
	bne cr7,L16
	.stabd	68,0,24
	li r0,0
	stb r0,96(r30)
	b L12
L16:
	.stabd	68,0,26
	lwz r2,88(r30)
	lwz r3,92(r30)
	addic r3,r3,1
	addze r2,r2
	stw r2,88(r30)
	stw r3,92(r30)
	b L12
L13:
	.stabd	68,0,29
	lbz r0,96(r30)
	extsb r0,r0
	cmpwi cr7,r0,0
	beq cr7,L18
	.stabd	68,0,30
	lwz r0,68(r30)
	slwi r2,r0,3
	addi r0,r30,64
	add r2,r2,r0
	addi r2,r2,48
	lfd f0,72(r30)
	stfd f0,0(r2)
	.stabd	68,0,31
	lwz r2,64(r30)
	lwz r3,68(r30)
	addic r3,r3,1
	addze r2,r2
	stw r2,64(r30)
	stw r3,68(r30)
L18:
	.stabd	68,0,33
	lwz r2,72(r30)
	lwz r3,76(r30)
	addic r3,r3,1
	addze r2,r2
	stw r2,72(r30)
	stw r3,76(r30)
	b L6
L7:
	.stabd	68,0,35
	addi r0,r30,104
	mr r3,r0
	bl L_time$stub
	.stabd	68,0,36
	lwz r2,104(r30)
	lwz r0,100(r30)
	subf r0,r0,r2
	addis r3,r31,ha16(LC1-"L00000000001$pb")
	la r3,lo16(LC1-"L00000000001$pb")(r3)
	mr r4,r0
	bl L_printf$stub
	.stabd	68,0,40
	li r0,0
	.stabd	68,0,41
LBE2:
	mr r3,r0
	lwz r1,0(r1)
	lwz r0,8(r1)
	mtlr r0
	lmw r30,-8(r1)
	blr
	.stabs	"main:F(0,1)=r(0,1);-2147483648;2147483647;",36,0,8,_main
	.stabs	"int:t(0,1)",128,0,0,0
	.stabs	"argc:p(0,1)",160,0,8,800168
	.stabs	"argv:p(0,2)=*(0,3)=*(0,4)=k(0,5)=r(0,5);0;127;",160,0,8,800172
	.stabs	"char:t(0,5)",128,0,0,0
	.stabs	"i:(0,6)=@s64;r(0,6);0000000000000;01777777777777777777777;",128,0,9,64
	.stabs	"long long unsigned int:t(0,6)",128,0,0,0
	.stabs	"j:(0,6)",128,0,9,72
	.stabs	"k:(0,6)",128,0,9,80
	.stabs	"l:(0,6)",128,0,9,88
	.stabs	"test:(0,5)",128,0,10,96
	.stabs	"t1:(0,7)=(0,8)=r(0,8);-2147483648;2147483647;",128,0,11,100
	.stabs	"time_t:t(0,7)",128,0,133,0
	.stabs	"long int:t(0,8)",128,0,0,0
	.stabs	"t2:(0,7)",128,0,11,104
	.stabs	"tab:(0,9)=ar(0,10)=r(0,10);0000000000000;0037777777777;;0;99999;(0,6)",128,0,12,112
	.stabs	"long unsigned int:t(0,11)=r(0,11);0000000000000;0037777777777;",128,0,0,0
	.stabn	192,0,0,LBB2
	.stabn	224,0,0,LBE2
.section __TEXT,__text,regular,pure_instructions
	.align 2
Lscope0:
	.stabs	"",36,0,0,Lscope0-_main
	.stabs "",100,0,0,Letext
Letext:
.data
.section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32
	.align 2
L___umoddi3$stub:
	.indirect_symbol ___umoddi3
	mflr r0
	bcl 20,31,L0$___umoddi3
L0$___umoddi3:
	mflr r11
	addis r11,r11,ha16(L___umoddi3$lazy_ptr-L0$___umoddi3)
	mtlr r0
	lwzu r12,lo16(L___umoddi3$lazy_ptr-L0$___umoddi3)(r11)
	mtctr r12
	bctr
.data
.lazy_symbol_pointer
L___umoddi3$lazy_ptr:
	.indirect_symbol ___umoddi3
	.long dyld_stub_binding_helper
.data
.section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32
	.align 2
L_sqrt$stub:
	.indirect_symbol _sqrt
	mflr r0
	bcl 20,31,L0$_sqrt
L0$_sqrt:
	mflr r11
	addis r11,r11,ha16(L_sqrt$lazy_ptr-L0$_sqrt)
	mtlr r0
	lwzu r12,lo16(L_sqrt$lazy_ptr-L0$_sqrt)(r11)
	mtctr r12
	bctr
.data
.lazy_symbol_pointer
L_sqrt$lazy_ptr:
	.indirect_symbol _sqrt
	.long dyld_stub_binding_helper
.data
.section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32
	.align 2
L___floatdidf$stub:
	.indirect_symbol ___floatdidf
	mflr r0
	bcl 20,31,L0$___floatdidf
L0$___floatdidf:
	mflr r11
	addis r11,r11,ha16(L___floatdidf$lazy_ptr-L0$___floatdidf)
	mtlr r0
	lwzu r12,lo16(L___floatdidf$lazy_ptr-L0$___floatdidf)(r11)
	mtctr r12
	bctr
.data
.lazy_symbol_pointer
L___floatdidf$lazy_ptr:
	.indirect_symbol ___floatdidf
	.long dyld_stub_binding_helper
.data
.section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32
	.align 2
L___cmpdi2$stub:
	.indirect_symbol ___cmpdi2
	mflr r0
	bcl 20,31,L0$___cmpdi2
L0$___cmpdi2:
	mflr r11
	addis r11,r11,ha16(L___cmpdi2$lazy_ptr-L0$___cmpdi2)
	mtlr r0
	lwzu r12,lo16(L___cmpdi2$lazy_ptr-L0$___cmpdi2)(r11)
	mtctr r12
	bctr
.data
.lazy_symbol_pointer
L___cmpdi2$lazy_ptr:
	.indirect_symbol ___cmpdi2
	.long dyld_stub_binding_helper
.data
.section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32
	.align 2
L_time$stub:
	.indirect_symbol _time
	mflr r0
	bcl 20,31,L0$_time
L0$_time:
	mflr r11
	addis r11,r11,ha16(L_time$lazy_ptr-L0$_time)
	mtlr r0
	lwzu r12,lo16(L_time$lazy_ptr-L0$_time)(r11)
	mtctr r12
	bctr
.data
.lazy_symbol_pointer
L_time$lazy_ptr:
	.indirect_symbol _time
	.long dyld_stub_binding_helper
.data
.section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32
	.align 2
L_printf$stub:
	.indirect_symbol _printf
	mflr r0
	bcl 20,31,L0$_printf
L0$_printf:
	mflr r11
	addis r11,r11,ha16(L_printf$lazy_ptr-L0$_printf)
	mtlr r0
	lwzu r12,lo16(L_printf$lazy_ptr-L0$_printf)(r11)
	mtctr r12
	bctr
.data
.lazy_symbol_pointer
L_printf$lazy_ptr:
	.indirect_symbol _printf
	.long dyld_stub_binding_helper
```


----------



## molgow (13 Janvier 2005)

Tout autre chose que les nombres premiers en Perl, voici un client-serveur de Peer-2-Peer en 6 lignes de code de Perl !


```
$/=$_;$,=shift;$w=$a=shift;$k{+shift}=1;socket S,2,1,6;bind S,&a;for(listen
S,5;$SIG{ALRM}=\&i;m! (\S+) ([e-i])([^/]*)/!s&&($k{$w=$1}=$,eq$`)&&&$2){alarm
9;(accept(C,S),alarm 0)?read C,$_,1e6:($_="$, $a f".shift)}sub i{}sub t{socket
C,2,1,6;$k{$w}&&=(connect C,&a)?print C"$, ".pop:0;close C}sub h{t"$_ i/"for
keys%k}sub a{$w=~/:/;pack'CxnC4x8',2,$',split'\.',$`}sub f{$w=$_,t"$1 $3/"for
keys%k}sub e{open C,'>',$3;print C $'}sub g{open(C,'<',$3)&&t"$a e$3/".<C>;&h}
```

Pour ceux qui aiment le Perl, aller voir la version documentée, on peut y apprendre des trucs intéressants pour la programmation en Perl


----------

