<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Number Archives - Onestring Lab</title>
	<atom:link href="https://onestringlab.com/tag/number/feed/" rel="self" type="application/rss+xml" />
	<link>https://onestringlab.com/tag/number/</link>
	<description>Kode Kreativitas Kopi</description>
	<lastBuildDate>Mon, 04 May 2026 08:37:42 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://onestringlab.com/wp-content/uploads/2021/10/cropped-osl-high-res-e1455499003866-32x32.jpg</url>
	<title>Number Archives - Onestring Lab</title>
	<link>https://onestringlab.com/tag/number/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Belajar Bahasa C &#8211; Is a number prime?</title>
		<link>https://onestringlab.com/is-a-number-prime/</link>
		
		<dc:creator><![CDATA[Rajo Intan]]></dc:creator>
		<pubDate>Tue, 06 Dec 2022 02:05:00 +0000</pubDate>
				<category><![CDATA[Codewars]]></category>
		<category><![CDATA[C Language]]></category>
		<category><![CDATA[Number]]></category>
		<category><![CDATA[Prime]]></category>
		<guid isPermaLink="false">https://onestringlab.com/?p=843</guid>

					<description><![CDATA[<p>Define a function that takes an integer argument and returns a logical value&#160;true&#160;or&#160;false&#160;depending on if the integer is a prime. Per Wikipedia, a prime number &#8230; </p>
<p>The post <a href="https://onestringlab.com/is-a-number-prime/">Belajar Bahasa C &#8211; Is a number prime?</a> appeared first on <a href="https://onestringlab.com">Onestring Lab</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Define a function that takes an integer argument and returns a logical value&nbsp;<code>true</code>&nbsp;or&nbsp;<code>false</code>&nbsp;depending on if the integer is a prime.</p>



<p>Per Wikipedia, a prime number ( or a prime ) is a natural number greater than&nbsp;<code>1</code>&nbsp;that has no positive divisors other than&nbsp;<code>1</code>&nbsp;and itself.</p>



<p>The problem link is <a href="http://bit.ly/3FpRUvp" target="_blank" rel="noreferrer noopener">here</a></p>



<h2 class="wp-block-heading">Requirements</h2>



<ul class="wp-block-list">
<li>You can assume you will be given an integer input.</li>



<li>You can not assume that the integer will be only positive. You may be given negative numbers as well ( or&nbsp;<code>0</code>&nbsp;).</li>



<li><strong>NOTE on performance</strong>: There are no fancy optimizations required, but still&nbsp;<em>the</em>&nbsp;most trivial solutions might time out. Numbers go up to 2^31 ( or similar, depending on language ). Looping all the way up to&nbsp;<code>n</code>, or&nbsp;<code>n/2</code>, will be too slow.</li>
</ul>



<h2 class="wp-block-heading">Example</h2>



<pre class="wp-block-code"><code lang="csharp" class="language-csharp">is_prime(1)  /* false */
is_prime(2)  /* true  */
is_prime(-1) /* false */</code></pre>



<h2 class="wp-block-heading">Sample Tests</h2>



<pre class="wp-block-code"><code lang="csharp" class="language-csharp line-numbers">#include &lt;criterion/criterion.h&gt;
#include &lt;stdbool.h&gt;

bool is_prime(int num);

Test(Sample_Test, basic_test)
{
    cr_assert_not(is_prime(0), "Your solution returned that 0 is prime, but it's not");
    cr_assert_not(is_prime(1), "Your solution returned that 1 is prime, but it's not");
    cr_assert(is_prime(2), "Your solution returned that 2 is not prime, but it is");
    cr_assert(is_prime(73), "Your solution returned that 73 is not prime, but it is");
    cr_assert_not(is_prime(75), "Your solution returned that 75 is prime, but it's not");
    cr_assert_not(is_prime(-1), "Your solution returned that -1 is prime, but it's not");
}

Test(Sample_Test, test_prime)
{
    cr_assert(is_prime(3), "Your solution returned that 3 is not prime, but it is");
    cr_assert(is_prime(5), "Your solution returned that 5 is not prime, but it is");
    cr_assert(is_prime(7), "Your solution returned that 7 is not prime, but it is");
    cr_assert(is_prime(41), "Your solution returned that 41 is not prime, but it is");
    cr_assert(is_prime(5099), "Your solution returned that 5099 is not prime, but it is");
}

Test(Sample_Test, test_not_prime)
{
    cr_assert_not(is_prime(4), "Your solution returned that 4 is prime, but it's not");
    cr_assert_not(is_prime(6), "Your solution returned that 6 is prime, but it's not");
    cr_assert_not(is_prime(8), "Your solution returned that 8 is prime, but it's not");
    cr_assert_not(is_prime(9), "Your solution returned that 9 is prime, but it's not");
    cr_assert_not(is_prime(45), "Your solution returned that 45 is prime, but it's not");
    cr_assert_not(is_prime(-5), "Your solution returned that -5 is prime, but it's not");
    cr_assert_not(is_prime(-8), "Your solution returned that -8 is prime, but it's not");
    cr_assert_not(is_prime(-41), "Your solution returned that -41 is prime, but it's not");
}

Test(Sample_Test, test_large)
{
    cr_assert_not(is_prime(247464361), "Your solution returned that 247464361 is prime, but it's not");
    cr_assert(is_prime(1634300119), "Your solution returned that 1634300119 is not prime, but it is");
}</code></pre>



<h2 class="wp-block-heading">Solution</h2>



<pre class="wp-block-code"><code lang="csharp" class="language-csharp line-numbers">#include &lt;stdbool.h&gt;

bool is_prime(int num)
{
  bool prime = true;
  int i, k;

  if (num &lt;= 1 || (num % 2 == 0 &amp;&amp; num != 2))
    prime = false;
  k = floor(sqrt(num));
  for (i = 3; i &lt;= k; i++)
    if (num % i == 0)
      prime = false;
  return prime;
}</code></pre>
<p>The post <a href="https://onestringlab.com/is-a-number-prime/">Belajar Bahasa C &#8211; Is a number prime?</a> appeared first on <a href="https://onestringlab.com">Onestring Lab</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
