Algoritmi Fundamentali 2
Algoritmi Fundamentali 2
Ali
ALGORITMi FUNDAMENTAL -
Interschimbaner valowts
due variatio valatal
1 .
a
anx &N auxiliar
a = b a < a+ b
b =
aux b < a -
b
a < a -
b
2 .
Greberance cifrelo mi numer natural i
citeste i
at'timp ** expecta
In
deterrhin ultima
cifra
↳getreate softe
-
m
(n/10]
= Il elimin ultima sifte
Aps .
1 : Sate
famefe un numat in eifrete in ordin
inverta (oglicdilul -
-- -
citeste
No
o to executa numeral actfel farmot
I
-t
ga
timp
Es
are
cifrel ordine
lote
in
age
M L
inversa ummoralia -
die ot
Apt2 fa meElaemet Mefree please
:
Sass
als si
in nor
citists W
750
· xoveFormer
zu Latelin
in
its
e
mate
Hix
- - -
ot
dain 2 % b= e all
Is
n = (w/10]
.
3 Cmmde ti Cmmme a done mymere naturale nemule a sib
Brin sideri sucessive
I
3 .
1 .
citete
E
a ,
b citerte ab
E ax b cat timp a + b executa
pcat
It
executi b to
timp dai b
Sm
a>
rt a Yo otunci a=a -
b
E altfel b= b -
a
de E a
emmdes- a
emmme -> f/emmele
4 .
n
4 1 .
.
cilerte w
S
d= n/2 executa
pentere i
-
scie
,
daio -/ d
·
Fi "unei scrie d
in
(in
4 2 . .
Algorith ecient -
care
generati divifii -
pereci priml-ultival
:
penultimat Samd)
,
aldrilea-
citeste y
timbay
↑
d = 1 i , executa
E
2++
peute
I
-
daii
nid=otunci
=
if (id 0)
I
d
serie / =
=
schiedd if d! dd)
ohnei
'coutcdda-
=
e++ cove
einn
altfel
ok=1 : // mesupun pric
is n eith out prim(intn)
↳ if (n=1) Relzu
-
for (intd =
2; d+ d< =
n; d +
+)
if (nyd 00K 0 ; =
= =
if Injd==o return o
;
if (0K==1) cout"s prim"; Return 1
;
the cout" nee prim"; y
Vitirea
reputati Is Melucarea for citire
pe motura and
↳ .
a n unmere
.
while (fin x)
↳enehereafter
y
1 Melecreati pex
donis
63
prelcrance a humere citite consecutiv
.
.
fixxx ; e
y
white
in
n prelecresti pe
x si y
X
y,
=
y
trei
64 . .
x
y;
=
y =
z;
y
I .
is ++
citeste
d=
S
w
Optimizover algritmu
2 ein wi
! =/executa d
cat
timpk n 2
I
=
-
= 0
while [dxdc =
n)
cat nyd executa 4k
timPk +
0 0
;
Trai kotunei
=
while/nyd
=
= = 0)
(1-edick
n =
[/d]
Io serisd k , if ;
d= dH yd + +
if I n ! =1) coutn'-' 1;
①I
Refultateimportede ...
k
① .
Anmiral divigoite linette s, nd=(k +1)(k2+1)
, ....
(kx+1)
↳ 2
.
. AuxildeWewe time up H de in
x-e) Here ene
(akx+1)
. .
A3
a ban
surpisttes to
urp
=
en ,
. . . .
emmme(a(b) =
u
8 4 .
.
Same
W
dirgontor his $ 4 .
Bage
.
to
bei
als 1D
Exn 29 puter
·
=
I -
b = 2 a
the
1
m
*
14 0 10 01
3
W
· I le
7 1
i
d
*
1000
ene 3 1
X /
0
10 on
·
I
=
*
10000
entert
-
w
,
b gata
I
X E O
Icat
Himp wo executi
Frie
.
RE %ob
R*
x
P
=
x +
* 1
p =
p
n = n/b
X
bafei
-
all
No .
Conversia unserelor naturale din Bagob in Baja 10 (2c = b( =
9)
-puteli
1AC+ 1x *
exentocb/x e
E m= On X
=
12 + 0* C + =
**
en
b 4 D
13(10)
=
2 1 +
0+ +
=
O 2
citests nib
I !) is
I
"
X O
1
p< to executa
cat
timp
Fre
c= m % 14
Xt **
Igata!
X 5
p
b
p px
N in
terment
11 .
1 .
Generance
plimilos a Fibonacci
citete e
·
Datin =
1 otunei Scie 1
altfel scie a, d
↳
.
I
executi
penbu it 3 i
,
In
c = n +
b
scrie C
ac b
btC
M 2
. .
Sa se
verifice daio un numer xete termen Fibonacci
Ex=S :
se va afiraL
DA
x = 7 :
Se va
afisa Nr
citerte X
I
a51
b51
b<x executa
cat
timp
Is
25 a+ b
a b =
b 5 C
12 .
aa /Saw
al= =
n. = n .
=
.
O
0 M
1024 1= 1024
.
.
citeste a ,
w
p1
executa
cat-timp to
I
daso n % 2 =
0 otunei as axa
n
= n/2
altfel p =p *a
n = n -
1