facile
Multiple
Soient \(a\) et \(b\) deux entiers positifs.
On dit que \(a\) est un multiple de \(b\) s'il existe un autre entier \(k\) tel que \(a =k \times b\) .
On demande d'écrire une fonction est_multiple
qui :
prend en paramètres deux entiers strictement positifs a
et b
;
renvoie True
si le premier est un multiple du second, False
dans le cas contraire.
Python propose une solution native détaillée ci-dessous pour répondre à cette question.
Solution native
Il suffit en effet de vérifier que le reste de la division euclidienne de \(a\) par \(b\) vaut \(0\) .
L'expression Python renvoyant le reste de la division de a
par b
est a % b
.
Une solution native serait donc :
🐍 Script Python def est_multiple ( a , b ):
return a % b == 0
Cette solution ne permet toutefois pas de répondre au cas \(b = 0\) (il est impossible de diviser par \(0\) ).
On propose de raisonner différemment. Pour ce faire, on gère immédiatement le cas \(b = 0\) en observant que le seul multiple de \(b = 0\) est \(a = 0\) . Ce cas particulier étant traité :
on crée une variable multiple
prenant initialement la valeur 0
;
tant que multiple
est strictement inférieur à a
, on ajoute b
à la valeur de multiple
;
en fin de boucle on teste l'égalité de multiple
et a
: s'ils sont égaux, a
est un multiple de b
. Sinon, a
n'est pas un multiple de b
.
Exemples
>>> est_multiple ( 0 , 0 )
True
>>> est_multiple ( 5 , 0 )
False
>>> est_multiple ( 10 , 2 )
True
>>> est_multiple ( 10 , 10 )
True
>>> est_multiple ( 2 , 10 )
False
>>> est_multiple ( 7 , 3 )
False
Version vide Version à compléter
.128013lSet-d5f18umag,_F/R=in
6)yàqPhcL(bEsx.p;r4'90"ovT+w73êOk:é 2030a07080h0p050E0#0z050h0E0E0o0O080p0H0O020r030E0f0g0g0h0J0u02060P050f0_0P0q0#000h0g0H0I0#0n07130J0w0f070E030m101214160~0H02031B1u1E0m1B0~0a0p0Q0.0:0=0@0y0p0i0y051S0y080|030)0C05071N0;0?0O1R1T1V1T081#1%1Z080J1C080y0.190E0H0h0q0@0$0O1(1P0O0c0+070q1h071Z1~20251*281%2b0g2d02040#0x0J0P0H0P0E0p1c1e0'1|0J0J070z2y1u2f0q1C0m1`2K1@1_1^1!0a2h0@1V0q2a2v1Z1K1M0/1)2U0p2W0q0P2!1Z0H2D1C2I2K2;0 1 1e2$262*0J13050|0#0d2H2^0}2@2g2`1*2|2~300$3320352I2T0O3a0h2 020#0V3e2J0~3h380@3k3m0#0K3q3g2^3i3w300b3A3s3C3u3j0P2}3l300s3H362_1O393M3b3n0U3R3t3U3v3W3O3n0e3!3J3$3L3N3x0M3+373-3E020d0N3=3T2%3.3X0d321v343I3?3~3^0d3d433f453}2{3'3m0d3p4b2J1F2/1u2!2N0a1_2S3K0z2+2n0%1L1C2.072:343A034v0'4D46260Y0|0'0c3A0#3S3D0c0|1s080k0g0f050_0H1%4F3#3~0{020B4(3,470|0h4.4K1*4+0j4Q4S3K0q0|0C4?4e4^0|0t0Z3H0#584R4)4L0|0p4P4k3n4|3@4 4{5b1*0P0|0o0o5l4/260g0p0|3{5g5i4*0|565g0r595G5a5t1*4M025e5s4@3v4;5O520@5o025q5S3i5v5x513i4+5D2;5F5H5+5A5c022D080f0J0q5Y3K0Y0z0|0R0J1r575+5I5P0O5L070,075$3K5'60615G5-5K0|5:5=5@5g625T645{020l3l0E685E6e5m5Q024Z4#0`4'6l6f5U5p5^3-5!025y5)6w5J0@5L0T1R6D2;6m3D0|6A4$6U346W3K5V004#0I6I4:024=5z6x0O6b6v616F3j6Y4!6!6u6V6_5V5r6E6;4~6z6|6C6~6$700|0S6,2{5k6@586_5L5N736P6`766B4%793f6%3-71726 745R6:7n6?6N6^6;5L6i5?7e5K6p5}5 7h7u3~6567693-7D446d7j6h0(6j7K6Q6p6r7T5E1u4H4C4m7.0m4p1u084r7?2Q2L0h1$7:4p1A4J6n2D0g0k0c0h0Y070k0y0V0|1m1o1q1s0#5(4E1H351B0A0h0#0E191b0p1d0#0H071b0#0W1@1'3l050!0i0!070G1F8m020D1e070c0c0(0j0#0v0#0'0f0F0#2D2.0p6t0E8U8v8Q8S2F1r0#0f1e084W0#0(0#050L8v2D0q0Q0P0p1'4G4w3i1,1U1W1Y813i5`5|5~7s4l0m7-3n2w0.660f0p090z0p8^0E080#0!0Q3l0f0!8V4R7-961W1.1X2e7n9d027N9g2K9j8^9v942A3K979H9a7j7(6s9O7,4w9k0L0+9t9v9x9z9B8W9T1'9V9G999J639L7)6t4F9i9'8K8l800X1e8x8z0a1d0z8X0p2D2F1n2a9v8~900u070J8`1'0J0!104#0h9v0a0f0#8?9u8i8L1J1L9F1-9^9b3K2j2a2c0|2p060z0J0`9v0x0u1`1d4F4B812=4E9j7Z024O7%3j4U024W4Y777r7U5B4,a;7f6.a@53024`7m6375507B634+556c7Q5.7l7y7nb0a)7wa)6K6M4E6;7W4c7Y7G7!5;7Ja~6n756/ba63bebr5Z5w6Lb66_756Z78bd6Hby3KbgbCbn026S29a)bEa/6#7t7b026)086+bJ5ja_b26nbk3rbmbb6{7qbU2Jb75nbIbvbsb-6}bH027db#6-b17E7ibN7I6kb@6X7pb`b~26bxc64}7A5)9%4I7/2K7 4o0(0*0,02.
.128013lSet-d5f18umag,_F/R=in
6)yàqPhcL(bEsx.p;r4'90"ovT+w73êOk:é 2030a07080h0p050E0#0z050h0E0E0o0O080p0H0O020r030E0f0g0g0h0J0u02060P050f0_0P0q0#000h0g0H0I0#0n07130J0w0f070E030m101214160~0H02031B1u1E0m1B0~0a0p0Q0.0:0=0@0y0p0i0y051S0y080|030)0C05071N0;0?0O1R1T1V1T081#1%1Z080J1C080y0.190E0H0h0q0@0$0O1(1P0O0c0+070q1h071Z1~20251*281%2b0g2d02040#0x0J0P0H0P0E0p1c1e0'1|0J0J070z2y1u2f0q1C0m1`2K1@1_1^1!0a2h0@1V0q2a2v1Z1K1M0/1)2U0p2W0q0P2!1Z0H2D1C2I2K2;0 1 1e2$262*0J13050|0#0d2H2^0}2@2g2`1*2|2~300$3320352I2T0O3a0h2 020#0V3e2J0~3h380@3k3m0#0K3q3g2^3i3w300b3A3s3C3u3j0P2}3l300s3H362_1O393M3b3n0U3R3t3U3v3W3O3n0e3!3J3$3L3N3x0M3+373-3E020d0N3=3T2%3.3X0d321v343I3?3~3^0d3d433f453}2{3'3m0d3p4b2J1F2/1u2!2N0a1_2S3K0z2+2n0%1L1C2.072:343A034v0'4D46260Y0|0'0c3A0#3S3D0c0|1s080k0g0f050_0H1%4F3#3~0{020B4(3,470|0h4.4K1*4+0j4Q4S3K0q0|0C4?4e4^0|0t0Z3H0#584R4)4L0|0p4P4k3n4|3@4 4{5b1*0P0|0o0o5l4/260g0p0|3{5g5i4*0|565g0r595G5a5t1*4M025e5s4@3v4;5O520@5o025q5S3i5v5x513i4+5D2;5F5H5+5A5c022D080f0J0q5Y3K0Y0z0|0R0J1r575+5I5P0O5L070,075$3K5'60615G5-5K0|5:5=5@5g625T645{020l3l0E685E6e5m5Q024Z4#0`4'6l6f5U5p5^3-5!025y5)6w5J0@5L0T1R6D2;6m3D0|6A4$6U346W3K5V004#0I6I4:024=5z6x0O6b6v616F3j6Y4!6!6u6V6_5V5r6E6;4~6z6|6C6~6$700|0S6,2{5k6@586_5L5N736P6`766B4%793f6%3-71726 745R6:7n6?6N6^6;5L6i5?7e5K6p5}5 7h7u3~6567693-7D446d7j6h0(6j7K6Q6p6r7T5E1u4H4C4m7.0m4p1u084r7?2Q2L0h1$7:4p1A4J6n2D0g0k0c0h0Y070k0y0V0|1m1o1q1s0#5(4E1H351B0A0h0#0E191b0p1d0#0H071b0#0W1@1'3l050!0i0!070G1F8m020D1e070c0c0(0j0#0v0#0'0f0F0#2D2.0p6t0E8U8v8Q8S2F1r0#0f1e084W0#0(0#050L8v2D0q0Q0P0p1'4G4w3i1,1U1W1Y813i5`5|5~7s4l0m7-3n2w0.660f0p090z0p8^0E080#0!0Q3l0f0!8V4R7-961W1.1X2e7n9d027N9g2K9j8^9v942A3K979H9a7j7(6s9O7,4w9k0L0+9t9v9x9z9B8W9T1'9V9G999J639L7)6t4F9i9'8K8l800X1e8x8z0a1d0z8X0p2D2F1n2a9v8~900u070J8`1'0J0!104#0h9v0a0f0#8?9u8i8L1J1L9F1-9^9b3K2j2a2c0|2p060z0J0`9v0x0u1`1d4F4B812=4E9j7Z024O7%3j4U024W4Y777r7U5B4,a;7f6.a@53024`7m6375507B634+556c7Q5.7l7y7nb0a)7wa)6K6M4E6;7W4c7Y7G7!5;7Ja~6n756/ba63bebr5Z5w6Lb66_756Z78bd6Hby3KbgbCbn026S29a)bEa/6#7t7b026)086+bJ5ja_b26nbk3rbmbb6{7qbU2Jb75nbIbvbsb-6}bH027db#6-b17E7ibN7I6kb@6X7pb`b~26bxc64}7A5)9%4I7/2K7 4o0(0*0,02.
# Tests
(insensible à la casse)(Ctrl+I)