TheAlgorithms-C/db/d80/problem__20_2sol1_8c.html

408 lines
34 KiB
HTML
Raw Normal View History

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.9.5"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C: project_euler/problem_20/sol1.c File Reference</title>
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../resize.js"></script>
<script type="text/javascript" src="../../navtreedata.js"></script>
<script type="text/javascript" src="../../navtree.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr id="projectrow">
<td id="projectalign">
<div id="projectname">Algorithms_in_C<span id="projectnumber">&#160;1.0.0</span>
</div>
<div id="projectbrief">Set of algorithms implemented in C.</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.5 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
/* @license-end */
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
initMenu('../../',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(document).ready(function(){initNavTree('db/d80/problem__20_2sol1_8c.html','../../'); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>
<div class="header">
<div class="summary">
<a href="#nested-classes">Data Structures</a> &#124;
<a href="#typedef-members">Typedefs</a> &#124;
<a href="#func-members">Functions</a> </div>
<div class="headertitle"><div class="title">sol1.c File Reference</div></div>
</div><!--header-->
<div class="contents">
<p><a href="https://projecteuler.net/problem=20" target="_blank">Problem 20</a> solution
<a href="#details">More...</a></p>
<div class="textblock"><code>#include &lt;stdio.h&gt;</code><br />
<code>#include &lt;stdlib.h&gt;</code><br />
<code>#include &lt;time.h&gt;</code><br />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for sol1.c:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d9/d7e/problem__20_2sol1_8c__incl.svg" width="243" height="124"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="nested-classes" name="nested-classes"></a>
Data Structures</h2></td></tr>
<tr class="memitem:"><td class="memItemLeft" align="right" valign="top">struct &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../dc/d77/struct__big__int.html">_big_int</a></td></tr>
<tr class="memdesc:"><td class="mdescLeft">&#160;</td><td class="mdescRight">store arbitratily large integer values as a linked list of digits. <a href="../../dc/d77/struct__big__int.html#details">More...</a><br /></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="typedef-members" name="typedef-members"></a>
Typedefs</h2></td></tr>
<tr class="memitem:a62cbbd106aad52daadb9403a07d0da32"><td class="memItemLeft" align="right" valign="top"><a id="a62cbbd106aad52daadb9403a07d0da32" name="a62cbbd106aad52daadb9403a07d0da32"></a>
typedef struct <a class="el" href="../../dc/d77/struct__big__int.html">_big_int</a>&#160;</td><td class="memItemRight" valign="bottom"><b>big_int</b></td></tr>
<tr class="memdesc:a62cbbd106aad52daadb9403a07d0da32"><td class="mdescLeft">&#160;</td><td class="mdescRight">store arbitratily large integer values as a linked list of digits. <br /></td></tr>
<tr class="separator:a62cbbd106aad52daadb9403a07d0da32"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="func-members" name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:a0dd8af03e6cc8187e996db564ff90960"><td class="memItemLeft" align="right" valign="top"><a class="el" href="../../db/d80/problem__20_2sol1_8c.html#a62cbbd106aad52daadb9403a07d0da32">big_int</a> *&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../db/d80/problem__20_2sol1_8c.html#a0dd8af03e6cc8187e996db564ff90960">add_digit</a> (<a class="el" href="../../db/d80/problem__20_2sol1_8c.html#a62cbbd106aad52daadb9403a07d0da32">big_int</a> *digit, char value)</td></tr>
<tr class="memdesc:a0dd8af03e6cc8187e996db564ff90960"><td class="mdescLeft">&#160;</td><td class="mdescRight">Function that allocates memory to add another digit at the MSB. <a href="../../db/d80/problem__20_2sol1_8c.html#a0dd8af03e6cc8187e996db564ff90960">More...</a><br /></td></tr>
<tr class="separator:a0dd8af03e6cc8187e996db564ff90960"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a54a02c4b963fdb16f24959e0137763f1"><td class="memItemLeft" align="right" valign="top">char&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../db/d80/problem__20_2sol1_8c.html#a54a02c4b963fdb16f24959e0137763f1">remove_digits</a> (<a class="el" href="../../db/d80/problem__20_2sol1_8c.html#a62cbbd106aad52daadb9403a07d0da32">big_int</a> *digit, int N)</td></tr>
<tr class="memdesc:a54a02c4b963fdb16f24959e0137763f1"><td class="mdescLeft">&#160;</td><td class="mdescRight">Function to remove digits preceeding the current digit. <a href="../../db/d80/problem__20_2sol1_8c.html#a54a02c4b963fdb16f24959e0137763f1">More...</a><br /></td></tr>
<tr class="separator:a54a02c4b963fdb16f24959e0137763f1"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a3c04138a5bfe5d72780bb7e82a18e627"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../db/d80/problem__20_2sol1_8c.html#a3c04138a5bfe5d72780bb7e82a18e627">main</a> (int argc, char **argv)</td></tr>
<tr class="memdesc:a3c04138a5bfe5d72780bb7e82a18e627"><td class="mdescLeft">&#160;</td><td class="mdescRight">Main function. <a href="../../db/d80/problem__20_2sol1_8c.html#a3c04138a5bfe5d72780bb7e82a18e627">More...</a><br /></td></tr>
<tr class="separator:a3c04138a5bfe5d72780bb7e82a18e627"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p ><a href="https://projecteuler.net/problem=20" target="_blank">Problem 20</a> solution </p>
<dl class="section author"><dt>Author</dt><dd><a href="https://github.com/kvedala" target="_blank">Krishna Vedala</a></dd></dl>
<p>Implementation uses a custom <code>big_int</code> structure that can store arbitrarily large integer numbers. </p>
</div><h2 class="groupheader">Function Documentation</h2>
<a id="a0dd8af03e6cc8187e996db564ff90960" name="a0dd8af03e6cc8187e996db564ff90960"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a0dd8af03e6cc8187e996db564ff90960">&#9670;&#160;</a></span>add_digit()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname"><a class="el" href="../../db/d80/problem__20_2sol1_8c.html#a62cbbd106aad52daadb9403a07d0da32">big_int</a> * add_digit </td>
<td>(</td>
<td class="paramtype"><a class="el" href="../../db/d80/problem__20_2sol1_8c.html#a62cbbd106aad52daadb9403a07d0da32">big_int</a> *&#160;</td>
<td class="paramname"><em>digit</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">char&#160;</td>
<td class="paramname"><em>value</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Function that allocates memory to add another digit at the MSB. </p>
<div class="fragment"><div class="line"><span class="lineno"> 38</span>{</div>
<div class="line"><span class="lineno"> 39</span> <span class="keywordflow">if</span> (digit == NULL)</div>
<div class="line"><span class="lineno"> 40</span> {</div>
<div class="line"><span class="lineno"> 41</span> digit = (<a class="code hl_struct" href="../../dc/d77/struct__big__int.html">big_int</a> *)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(<span class="keyword">sizeof</span>(<a class="code hl_struct" href="../../dc/d77/struct__big__int.html">big_int</a>));</div>
<div class="line"><span class="lineno"> 42</span> <span class="keywordflow">if</span> (!digit)</div>
<div class="line"><span class="lineno"> 43</span> {</div>
<div class="line"><span class="lineno"> 44</span> perror(<span class="stringliteral">&quot;Unable to allocate memory!&quot;</span>);</div>
<div class="line"><span class="lineno"> 45</span> <span class="keywordflow">return</span> NULL;</div>
<div class="line"><span class="lineno"> 46</span> }</div>
<div class="line"><span class="lineno"> 47</span> digit-&gt;<a class="code hl_variable" href="../../dc/d77/struct__big__int.html#a273ee73fd755f2a99512cca5f0e09008">value</a> = value;</div>
<div class="line"><span class="lineno"> 48</span> digit-&gt;<a class="code hl_variable" href="../../dc/d77/struct__big__int.html#a187538b984c86d7cfdb13e297e7f3564">next_digit</a> = NULL;</div>
<div class="line"><span class="lineno"> 49</span> digit-&gt;<a class="code hl_variable" href="../../dc/d77/struct__big__int.html#ad8405989a924410942b39ec0e9fef30b">prev_digit</a> = NULL;</div>
<div class="line"><span class="lineno"> 50</span> </div>
<div class="line"><span class="lineno"> 51</span> <span class="keywordflow">return</span> digit;</div>
<div class="line"><span class="lineno"> 52</span> }</div>
<div class="line"><span class="lineno"> 53</span> </div>
<div class="line"><span class="lineno"> 54</span> <span class="keywordflow">if</span> (digit-&gt;<a class="code hl_variable" href="../../dc/d77/struct__big__int.html#a187538b984c86d7cfdb13e297e7f3564">next_digit</a>)</div>
<div class="line"><span class="lineno"> 55</span> {</div>
<div class="line"><span class="lineno"> 56</span> digit-&gt;<a class="code hl_variable" href="../../dc/d77/struct__big__int.html#a187538b984c86d7cfdb13e297e7f3564">next_digit</a>-&gt;<a class="code hl_variable" href="../../dc/d77/struct__big__int.html#a273ee73fd755f2a99512cca5f0e09008">value</a> = value;</div>
<div class="line"><span class="lineno"> 57</span> <span class="keywordflow">return</span> digit-&gt;<a class="code hl_variable" href="../../dc/d77/struct__big__int.html#a187538b984c86d7cfdb13e297e7f3564">next_digit</a>;</div>
<div class="line"><span class="lineno"> 58</span> }</div>
<div class="line"><span class="lineno"> 59</span> </div>
<div class="line"><span class="lineno"> 60</span> digit-&gt;<a class="code hl_variable" href="../../dc/d77/struct__big__int.html#a187538b984c86d7cfdb13e297e7f3564">next_digit</a> = (<a class="code hl_struct" href="../../dc/d77/struct__big__int.html">big_int</a> *)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(<span class="keyword">sizeof</span>(<a class="code hl_struct" href="../../dc/d77/struct__big__int.html">big_int</a>));</div>
<div class="line"><span class="lineno"> 61</span> <span class="keywordflow">if</span> (digit-&gt;<a class="code hl_variable" href="../../dc/d77/struct__big__int.html#a187538b984c86d7cfdb13e297e7f3564">next_digit</a> == NULL)</div>
<div class="line"><span class="lineno"> 62</span> {</div>
<div class="line"><span class="lineno"> 63</span> perror(<span class="stringliteral">&quot;Unable to allocate memory!&quot;</span>);</div>
<div class="line"><span class="lineno"> 64</span> <span class="keywordflow">return</span> NULL;</div>
<div class="line"><span class="lineno"> 65</span> }</div>
<div class="line"><span class="lineno"> 66</span> digit-&gt;<a class="code hl_variable" href="../../dc/d77/struct__big__int.html#a187538b984c86d7cfdb13e297e7f3564">next_digit</a>-&gt;<a class="code hl_variable" href="../../dc/d77/struct__big__int.html#a273ee73fd755f2a99512cca5f0e09008">value</a> = value;</div>
<div class="line"><span class="lineno"> 67</span> digit-&gt;<a class="code hl_variable" href="../../dc/d77/struct__big__int.html#a187538b984c86d7cfdb13e297e7f3564">next_digit</a>-&gt;<a class="code hl_variable" href="../../dc/d77/struct__big__int.html#a187538b984c86d7cfdb13e297e7f3564">next_digit</a> = NULL;</div>
<div class="line"><span class="lineno"> 68</span> digit-&gt;<a class="code hl_variable" href="../../dc/d77/struct__big__int.html#a187538b984c86d7cfdb13e297e7f3564">next_digit</a>-&gt;<a class="code hl_variable" href="../../dc/d77/struct__big__int.html#ad8405989a924410942b39ec0e9fef30b">prev_digit</a> = digit;</div>
<div class="line"><span class="lineno"> 69</span> <span class="keywordflow">return</span> digit-&gt;<a class="code hl_variable" href="../../dc/d77/struct__big__int.html#a187538b984c86d7cfdb13e297e7f3564">next_digit</a>;</div>
<div class="line"><span class="lineno"> 70</span>}</div>
<div class="ttc" id="amalloc__dbg_8h_html_a725f50ecaf1959d96de79b36b4788fee"><div class="ttname"><a href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a></div><div class="ttdeci">#define malloc(bytes)</div><div class="ttdoc">This macro replace the standard malloc function with malloc_dbg.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.h:18</div></div>
<div class="ttc" id="astruct__big__int_html"><div class="ttname"><a href="../../dc/d77/struct__big__int.html">_big_int</a></div><div class="ttdoc">store arbitratily large integer values as a linked list of digits.</div><div class="ttdef"><b>Definition:</b> sol1.c:18</div></div>
<div class="ttc" id="astruct__big__int_html_a187538b984c86d7cfdb13e297e7f3564"><div class="ttname"><a href="../../dc/d77/struct__big__int.html#a187538b984c86d7cfdb13e297e7f3564">_big_int::next_digit</a></div><div class="ttdeci">struct _big_int * next_digit</div><div class="ttdoc">hundreds place</div><div class="ttdef"><b>Definition:</b> sol1.c:20</div></div>
<div class="ttc" id="astruct__big__int_html_a273ee73fd755f2a99512cca5f0e09008"><div class="ttname"><a href="../../dc/d77/struct__big__int.html#a273ee73fd755f2a99512cca5f0e09008">_big_int::value</a></div><div class="ttdeci">char value</div><div class="ttdoc">tens place (single digit)</div><div class="ttdef"><b>Definition:</b> sol1.c:19</div></div>
<div class="ttc" id="astruct__big__int_html_ad8405989a924410942b39ec0e9fef30b"><div class="ttname"><a href="../../dc/d77/struct__big__int.html#ad8405989a924410942b39ec0e9fef30b">_big_int::prev_digit</a></div><div class="ttdeci">struct _big_int * prev_digit</div><div class="ttdoc">units place</div><div class="ttdef"><b>Definition:</b> sol1.c:21</div></div>
</div><!-- fragment -->
</div>
</div>
<a id="a3c04138a5bfe5d72780bb7e82a18e627" name="a3c04138a5bfe5d72780bb7e82a18e627"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a3c04138a5bfe5d72780bb7e82a18e627">&#9670;&#160;</a></span>main()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int main </td>
<td>(</td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>argc</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">char **&#160;</td>
<td class="paramname"><em>argv</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Main function. </p>
<div class="fragment"><div class="line"><span class="lineno"> 96</span>{</div>
<div class="line"><span class="lineno"> 97</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> N = 5;</div>
<div class="line"><span class="lineno"> 98</span> <a class="code hl_struct" href="../../dc/d77/struct__big__int.html">big_int</a> *ptr = <a class="code hl_function" href="../../db/d80/problem__20_2sol1_8c.html#a0dd8af03e6cc8187e996db564ff90960">add_digit</a>(NULL, 1); <span class="comment">/* start with 1 */</span></div>
<div class="line"><span class="lineno"> 99</span> <span class="keyword">const</span> <a class="code hl_struct" href="../../dc/d77/struct__big__int.html">big_int</a> *ptr0 = ptr; <span class="comment">/* save the first location */</span></div>
<div class="line"><span class="lineno"> 100</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> sum_digits = 0;</div>
<div class="line"><span class="lineno"> 101</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> num_digits = 0;</div>
<div class="line"><span class="lineno"> 102</span> </div>
<div class="line"><span class="lineno"> 103</span> <span class="keywordflow">if</span> (argc == 2)</div>
<div class="line"><span class="lineno"> 104</span> N = atoi(argv[1]);</div>
<div class="line"><span class="lineno"> 105</span> </div>
<div class="line"><span class="lineno"> 106</span> clock_t start_time = clock();</div>
<div class="line"><span class="lineno"> 107</span> </div>
<div class="line"><span class="lineno"> 108</span> <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i = 1; i &lt;= N; i++)</div>
<div class="line"><span class="lineno"> 109</span> {</div>
<div class="line"><span class="lineno"> 110</span> <span class="keywordtype">int</span> carry = 0;</div>
<div class="line"><span class="lineno"> 111</span><span class="preprocessor">#ifdef DEBUG</span></div>
<div class="line"><span class="lineno"> 112</span> printf(<span class="stringliteral">&quot;%3d: &quot;</span>, i);</div>
<div class="line"><span class="lineno"> 113</span><span class="preprocessor">#endif</span></div>
<div class="line"><span class="lineno"> 114</span> ptr = (<a class="code hl_struct" href="../../dc/d77/struct__big__int.html">big_int</a> *)ptr0; <span class="comment">/* multiply every digit with i */</span></div>
<div class="line"><span class="lineno"> 115</span> <span class="keywordflow">while</span> (ptr)</div>
<div class="line"><span class="lineno"> 116</span> {</div>
<div class="line"><span class="lineno"> 117</span><span class="preprocessor">#ifdef DEBUG</span></div>
<div class="line"><span class="lineno"> 118</span> printf(<span class="stringliteral">&quot;%p\t&quot;</span>, ptr);</div>
<div class="line"><span class="lineno"> 119</span><span class="preprocessor">#endif</span></div>
<div class="line"><span class="lineno"> 120</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> tmp = ptr-&gt;<a class="code hl_variable" href="../../dc/d77/struct__big__int.html#a273ee73fd755f2a99512cca5f0e09008">value</a> * i + carry;</div>
<div class="line"><span class="lineno"> 121</span> <span class="keywordflow">if</span> (tmp &gt;= 10)</div>
<div class="line"><span class="lineno"> 122</span> {</div>
<div class="line"><span class="lineno"> 123</span> div_t tmp2 = div(tmp, 10);</div>
<div class="line"><span class="lineno"> 124</span> carry = tmp2.quot;</div>
<div class="line"><span class="lineno"> 125</span> tmp = tmp2.rem;</div>
<div class="line"><span class="lineno"> 126</span> }</div>
<div class="line"><span class="lineno"> 127</span> <span class="keywordflow">else</span></div>
<div class="line"><span class="lineno"> 128</span> carry = 0;</div>
<div class="line"><span class="lineno"> 129</span> </div>
<div class="line"><span class="lineno"> 130</span> <span class="keywordflow">if</span> (carry &gt; 0 &amp;&amp; ptr-&gt;<a class="code hl_variable" href="../../dc/d77/struct__big__int.html#a187538b984c86d7cfdb13e297e7f3564">next_digit</a> == NULL)</div>
<div class="line"><span class="lineno"> 131</span> <a class="code hl_function" href="../../db/d80/problem__20_2sol1_8c.html#a0dd8af03e6cc8187e996db564ff90960">add_digit</a>(ptr, 0);</div>
<div class="line"><span class="lineno"> 132</span> </div>
<div class="line"><span class="lineno"> 133</span> ptr-&gt;<a class="code hl_variable" href="../../dc/d77/struct__big__int.html#a273ee73fd755f2a99512cca5f0e09008">value</a> = tmp;</div>
<div class="line"><span class="lineno"> 134</span> </div>
<div class="line"><span class="lineno"> 135</span> <span class="keywordflow">if</span> (i == N)</div>
<div class="line"><span class="lineno"> 136</span> <span class="comment">/*</span></div>
<div class="line"><span class="lineno"> 137</span><span class="comment"> * sum digits on the last iteration</span></div>
<div class="line"><span class="lineno"> 138</span><span class="comment"> * this avoid having another loop over all digits</span></div>
<div class="line"><span class="lineno"> 139</span><span class="comment"> */</span></div>
<div class="line"><span class="lineno"> 140</span> sum_digits += tmp;</div>
<div class="line"><span class="lineno"> 141</span> </div>
<div class="line"><span class="lineno"> 142</span> <span class="keywordflow">if</span> (ptr-&gt;<a class="code hl_variable" href="../../dc/d77/struct__big__int.html#a187538b984c86d7cfdb13e297e7f3564">next_digit</a>)</div>
<div class="line"><span class="lineno"> 143</span> <span class="comment">/* more digits available */</span></div>
<div class="line"><span class="lineno"> 144</span> ptr = ptr-&gt;<a class="code hl_variable" href="../../dc/d77/struct__big__int.html#a187538b984c86d7cfdb13e297e7f3564">next_digit</a>;</div>
<div class="line"><span class="lineno"> 145</span> <span class="keywordflow">else</span></div>
<div class="line"><span class="lineno"> 146</span> <span class="comment">/* no more digits left - reached MSB */</span></div>
<div class="line"><span class="lineno"> 147</span> <span class="keywordflow">break</span>;</div>
<div class="line"><span class="lineno"> 148</span> }</div>
<div class="line"><span class="lineno"> 149</span><span class="preprocessor">#ifdef DEBUG</span></div>
<div class="line"><span class="lineno"> 150</span> printf(<span class="stringliteral">&quot;\n&quot;</span>);</div>
<div class="line"><span class="lineno"> 151</span><span class="preprocessor">#endif</span></div>
<div class="line"><span class="lineno"> 152</span> }</div>
<div class="line"><span class="lineno"> 153</span> </div>
<div class="line"><span class="lineno"> 154</span> clock_t end_time = clock();</div>
<div class="line"><span class="lineno"> 155</span> </div>
<div class="line"><span class="lineno"> 156</span><span class="preprocessor">#ifdef DEBUG</span></div>
<div class="line"><span class="lineno"> 157</span> printf(<span class="stringliteral">&quot;ptr = %p\n&quot;</span>, ptr);</div>
<div class="line"><span class="lineno"> 158</span> printf(<span class="stringliteral">&quot;%d! = &quot;</span>, N);</div>
<div class="line"><span class="lineno"> 159</span><span class="preprocessor">#endif</span></div>
<div class="line"><span class="lineno"> 160</span> </div>
<div class="line"><span class="lineno"> 161</span> <span class="comment">/* Notice that in the loop above, we make sure that at the end of the loop,</span></div>
<div class="line"><span class="lineno"> 162</span><span class="comment"> * ptr is pointing to the last digit. Thus we can avoid using another loop.</span></div>
<div class="line"><span class="lineno"> 163</span><span class="comment"> */</span></div>
<div class="line"><span class="lineno"> 164</span> <span class="comment">// ptr = &amp;my_int;</span></div>
<div class="line"><span class="lineno"> 165</span> <span class="comment">// /* move ptr to the MSB digit */</span></div>
<div class="line"><span class="lineno"> 166</span> <span class="comment">// while (ptr-&gt;next_digit)</span></div>
<div class="line"><span class="lineno"> 167</span> <span class="comment">// ptr = ptr-&gt;next_digit;</span></div>
<div class="line"><span class="lineno"> 168</span> <span class="keywordflow">do</span></div>
<div class="line"><span class="lineno"> 169</span> {</div>
<div class="line"><span class="lineno"> 170</span> putchar(ptr-&gt;<a class="code hl_variable" href="../../dc/d77/struct__big__int.html#a273ee73fd755f2a99512cca5f0e09008">value</a> + 0x30); <span class="comment">/* convert digit to ASCII char */</span></div>
<div class="line"><span class="lineno"> 171</span> ptr = ptr-&gt;<a class="code hl_variable" href="../../dc/d77/struct__big__int.html#ad8405989a924410942b39ec0e9fef30b">prev_digit</a>;</div>
<div class="line"><span class="lineno"> 172</span> num_digits++;</div>
<div class="line"><span class="lineno"> 173</span> } <span class="keywordflow">while</span> (ptr); <span class="comment">/* after coming to units place, there will be no valid ptr */</span></div>
<div class="line"><span class="lineno"> 174</span> </div>
<div class="line"><span class="lineno"> 175</span> printf(<span class="stringliteral">&quot;\nTime taken: %.4g millisecond\n&quot;</span>,</div>
<div class="line"><span class="lineno"> 176</span> 1e3 * (end_time - start_time) / CLOCKS_PER_SEC);</div>
<div class="line"><span class="lineno"> 177</span> printf(</div>
<div class="line"><span class="lineno"> 178</span> <span class="stringliteral">&quot;Digit Sum = %lu\tNumber of digits = %lu\tStorage space = %.3gkb\t \n&quot;</span>,</div>
<div class="line"><span class="lineno"> 179</span> sum_digits, num_digits, num_digits * <span class="keyword">sizeof</span>(<a class="code hl_struct" href="../../dc/d77/struct__big__int.html">big_int</a>) / 1024.0);</div>
<div class="line"><span class="lineno"> 180</span> </div>
<div class="line"><span class="lineno"> 181</span> <a class="code hl_function" href="../../db/d80/problem__20_2sol1_8c.html#a54a02c4b963fdb16f24959e0137763f1">remove_digits</a>((<a class="code hl_struct" href="../../dc/d77/struct__big__int.html">big_int</a> *)ptr0, -1);</div>
<div class="line"><span class="lineno"> 182</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 183</span>}</div>
<div class="ttc" id="aproblem__20_2sol1_8c_html_a0dd8af03e6cc8187e996db564ff90960"><div class="ttname"><a href="../../db/d80/problem__20_2sol1_8c.html#a0dd8af03e6cc8187e996db564ff90960">add_digit</a></div><div class="ttdeci">big_int * add_digit(big_int *digit, char value)</div><div class="ttdoc">Function that allocates memory to add another digit at the MSB.</div><div class="ttdef"><b>Definition:</b> sol1.c:37</div></div>
<div class="ttc" id="aproblem__20_2sol1_8c_html_a54a02c4b963fdb16f24959e0137763f1"><div class="ttname"><a href="../../db/d80/problem__20_2sol1_8c.html#a54a02c4b963fdb16f24959e0137763f1">remove_digits</a></div><div class="ttdeci">char remove_digits(big_int *digit, int N)</div><div class="ttdoc">Function to remove digits preceeding the current digit.</div><div class="ttdef"><b>Definition:</b> sol1.c:76</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../db/d80/problem__20_2sol1_8c_a3c04138a5bfe5d72780bb7e82a18e627_cgraph.svg" width="212" height="110"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="a54a02c4b963fdb16f24959e0137763f1" name="a54a02c4b963fdb16f24959e0137763f1"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a54a02c4b963fdb16f24959e0137763f1">&#9670;&#160;</a></span>remove_digits()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">char remove_digits </td>
<td>(</td>
<td class="paramtype"><a class="el" href="../../db/d80/problem__20_2sol1_8c.html#a62cbbd106aad52daadb9403a07d0da32">big_int</a> *&#160;</td>
<td class="paramname"><em>digit</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>N</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Function to remove digits preceeding the current digit. </p>
<div class="fragment"><div class="line"><span class="lineno"> 77</span>{</div>
<div class="line"><span class="lineno"> 78</span> <span class="keywordflow">if</span> (digit == NULL)</div>
<div class="line"><span class="lineno"> 79</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 80</span> </div>
<div class="line"><span class="lineno"> 81</span> <span class="keywordflow">if</span> (digit-&gt;<a class="code hl_variable" href="../../dc/d77/struct__big__int.html#a187538b984c86d7cfdb13e297e7f3564">next_digit</a> == NULL)</div>
<div class="line"><span class="lineno"> 82</span> {</div>
<div class="line"><span class="lineno"> 83</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(digit);</div>
<div class="line"><span class="lineno"> 84</span> digit = NULL;</div>
<div class="line"><span class="lineno"> 85</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 86</span> }</div>
<div class="line"><span class="lineno"> 87</span> </div>
<div class="line"><span class="lineno"> 88</span> <span class="keywordflow">if</span> (N &gt; 0)</div>
<div class="line"><span class="lineno"> 89</span> <span class="keywordflow">return</span> <a class="code hl_function" href="../../db/d80/problem__20_2sol1_8c.html#a54a02c4b963fdb16f24959e0137763f1">remove_digits</a>(digit-&gt;<a class="code hl_variable" href="../../dc/d77/struct__big__int.html#a187538b984c86d7cfdb13e297e7f3564">next_digit</a>, N - 1);</div>
<div class="line"><span class="lineno"> 90</span> </div>
<div class="line"><span class="lineno"> 91</span> <span class="keywordflow">return</span> <a class="code hl_function" href="../../db/d80/problem__20_2sol1_8c.html#a54a02c4b963fdb16f24959e0137763f1">remove_digits</a>(digit-&gt;<a class="code hl_variable" href="../../dc/d77/struct__big__int.html#a187538b984c86d7cfdb13e297e7f3564">next_digit</a>, 0);</div>
<div class="line"><span class="lineno"> 92</span>}</div>
<div class="ttc" id="amalloc__dbg_8h_html_a9cc854374299a1dd933bf62029761768"><div class="ttname"><a href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a></div><div class="ttdeci">#define free(ptr)</div><div class="ttdoc">This macro replace the standard free function with free_dbg.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.h:26</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../db/d80/problem__20_2sol1_8c_a54a02c4b963fdb16f24959e0137763f1_cgraph.svg" width="114" height="60"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="../../dir_b3d331eabd4234f247030c297aaf021c.html">project_euler</a></li><li class="navelem"><a class="el" href="../../dir_4991c92e00761dd3d764efa2906b4318.html">problem_20</a></li><li class="navelem"><a class="el" href="../../db/d80/problem__20_2sol1_8c.html">sol1.c</a></li>
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.5 </li>
</ul>
</div>
</body>
</html>