% Options for packages loaded elsewhere \PassOptionsToPackage{unicode}{hyperref} \PassOptionsToPackage{hyphens}{url} % \documentclass[ ]{article} \usepackage{amsmath,amssymb} \usepackage{lmodern} \usepackage{iftex} \ifPDFTeX \usepackage[T1]{fontenc} \usepackage[utf8]{inputenc} \usepackage{textcomp} % provide euro and other symbols \else % if luatex or xetex \usepackage{unicode-math} \defaultfontfeatures{Scale=MatchLowercase} \defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1} \fi % Use upquote if available, for straight quotes in verbatim environments \IfFileExists{upquote.sty}{\usepackage{upquote}}{} \IfFileExists{microtype.sty}{% use microtype if available \usepackage[]{microtype} \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts }{} \makeatletter \@ifundefined{KOMAClassName}{% if non-KOMA class \IfFileExists{parskip.sty}{% \usepackage{parskip} }{% else \setlength{\parindent}{0pt} \setlength{\parskip}{6pt plus 2pt minus 1pt}} }{% if KOMA class \KOMAoptions{parskip=half}} \makeatother \usepackage{xcolor} \IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available \IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}} \hypersetup{ hidelinks, pdfcreator={LaTeX via pandoc}} \urlstyle{same} % disable monospaced font for URLs \usepackage[margin=1in]{geometry} \usepackage{color} \usepackage{fancyvrb} \newcommand{\VerbBar}{|} \newcommand{\VERB}{\Verb[commandchars=\\\{\}]} \DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}} % Add ',fontsize=\small' for more characters per line \usepackage{framed} \definecolor{shadecolor}{RGB}{248,248,248} \newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}} \newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}} \newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}} \newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{#1}} \newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}} \newcommand{\BuiltInTok}[1]{#1} \newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}} \newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}} \newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}} \newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}} \newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}} \newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}} \newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}} \newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}} \newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}} \newcommand{\ExtensionTok}[1]{#1} \newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}} \newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}} \newcommand{\ImportTok}[1]{#1} \newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}} \newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}} \newcommand{\NormalTok}[1]{#1} \newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}} \newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}} \newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}} \newcommand{\RegionMarkerTok}[1]{#1} \newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}} \newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}} \newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}} \newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}} \newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}} \newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}} \usepackage{graphicx} \makeatletter \def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi} \def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi} \makeatother % Scale images if necessary, so that they will not overflow the page % margins by default, and it is still possible to overwrite the defaults % using explicit options in \includegraphics[width, height, ...]{} \setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio} % Set default figure placement to htbp \makeatletter \def\fps@figure{htbp} \makeatother \setlength{\emergencystretch}{3em} % prevent overfull lines \providecommand{\tightlist}{% \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}} \setcounter{secnumdepth}{-\maxdimen} % remove section numbering \ifLuaTeX \usepackage{selnolig} % disable illegal ligatures \fi \author{} \date{\vspace{-2.5em}} \begin{document} \hypertarget{uxe0-propos-du-calcul-de-pi}{% \section{À propos du calcul de pi}\label{uxe0-propos-du-calcul-de-pi}} \emph{Arnaud Legrand} \emph{25 juin 2018} \hypertarget{en-demandant-uxe0-la-lib-maths}{% \subsection{En demandant à la lib maths}\label{en-demandant-uxe0-la-lib-maths}} Mon ordinateur m'indique que π vaut \emph{approximativement} \begin{Shaded} \begin{Highlighting}[] \NormalTok{pi} \end{Highlighting} \end{Shaded} \begin{verbatim} ## [1] 3.141593 \end{verbatim} \hypertarget{en-utilisant-la-muxe9thode-des-aiguilles-de-buffon}{% \subsection{En utilisant la méthode des aiguilles de Buffon}\label{en-utilisant-la-muxe9thode-des-aiguilles-de-buffon}} Mais calculé avec la \textbf{méthode} des \href{https://fr.wikipedia.org/wiki/Aiguille_de_Buffon}{aiguilles de Buffon}, on obtiendrait comme \textbf{approximation} : \begin{Shaded} \begin{Highlighting}[] \FunctionTok{set.seed}\NormalTok{(}\DecValTok{42}\NormalTok{)} \NormalTok{N }\OtherTok{=} \DecValTok{100000} \NormalTok{x }\OtherTok{=} \FunctionTok{runif}\NormalTok{(N)} \NormalTok{theta }\OtherTok{=}\NormalTok{ pi}\SpecialCharTok{/}\DecValTok{2}\SpecialCharTok{*}\FunctionTok{runif}\NormalTok{(N)} \DecValTok{2}\SpecialCharTok{/}\NormalTok{(}\FunctionTok{mean}\NormalTok{(x}\SpecialCharTok{+}\FunctionTok{sin}\NormalTok{(theta)}\SpecialCharTok{\textgreater{}}\DecValTok{1}\NormalTok{))} \end{Highlighting} \end{Shaded} \begin{verbatim} ## [1] 3.14327 \end{verbatim} \hypertarget{avec-un-argument-fruxe9quentiel-de-surface}{% \subsection{Avec un argument ``fréquentiel'' de surface}\label{avec-un-argument-fruxe9quentiel-de-surface}} Sinon, une méthode plus simple à comprendre et ne faisant pas intervenir d'appel à la fonction sinus se base sur le fait que si X∼U(0,1) et Y∼U(0,1) alors P{[}X2+Y2≤1{]}=π/4 (voir \href{https://fr.wikipedia.org/wiki/M\%C3\%A9thode_de_Monte-Carlo\#D\%C3\%A9termination_de_la_valeur_de_\%CF\%80}{méthode de Monte Carlo sur Wikipedia}). Le code suivant illustre ce fait: \begin{Shaded} \begin{Highlighting}[] \FunctionTok{set.seed}\NormalTok{(}\DecValTok{42}\NormalTok{)} \NormalTok{N }\OtherTok{=} \DecValTok{1000} \NormalTok{df }\OtherTok{=} \FunctionTok{data.frame}\NormalTok{(}\AttributeTok{X =} \FunctionTok{runif}\NormalTok{(N), }\AttributeTok{Y =} \FunctionTok{runif}\NormalTok{(N))} \NormalTok{df}\SpecialCharTok{$}\NormalTok{Accept }\OtherTok{=}\NormalTok{ (df}\SpecialCharTok{$}\NormalTok{X}\SpecialCharTok{**}\DecValTok{2} \SpecialCharTok{+}\NormalTok{ df}\SpecialCharTok{$}\NormalTok{Y}\SpecialCharTok{**}\DecValTok{2} \SpecialCharTok{\textless{}=}\DecValTok{1}\NormalTok{)} \FunctionTok{library}\NormalTok{(ggplot2)} \end{Highlighting} \end{Shaded} \begin{verbatim} ## Warning: le package 'ggplot2' a été compilé avec la version R 4.3.3 \end{verbatim} \begin{Shaded} \begin{Highlighting}[] \FunctionTok{ggplot}\NormalTok{(df, }\FunctionTok{aes}\NormalTok{(}\AttributeTok{x=}\NormalTok{X,}\AttributeTok{y=}\NormalTok{Y,}\AttributeTok{color=}\NormalTok{Accept)) }\SpecialCharTok{+} \FunctionTok{geom\_point}\NormalTok{(}\AttributeTok{alpha=}\NormalTok{.}\DecValTok{2}\NormalTok{) }\SpecialCharTok{+} \FunctionTok{coord\_fixed}\NormalTok{() }\SpecialCharTok{+} \FunctionTok{theme\_bw}\NormalTok{()} \end{Highlighting} \end{Shaded} \includegraphics{toy_document_fr_files/figure-latex/unnamed-chunk-3-1.pdf} Il est alors aisé d'obtenir une approximation (pas terrible) de π en comptant combien de fois, en moyenne, X2+Y2 est inférieur à 1: \begin{Shaded} \begin{Highlighting}[] \DecValTok{4}\SpecialCharTok{*}\FunctionTok{mean}\NormalTok{(df}\SpecialCharTok{$}\NormalTok{Accept)} \end{Highlighting} \end{Shaded} \begin{verbatim} ## [1] 3.156 \end{verbatim} \end{document}