Skip to content

Instantly share code, notes, and snippets.

@margusmartsepp
Last active December 24, 2020 21:36
Show Gist options
  • Save margusmartsepp/e6f9db36ec6fb8104d040c2cf6b33e9c to your computer and use it in GitHub Desktop.
Save margusmartsepp/e6f9db36ec6fb8104d040c2cf6b33e9c to your computer and use it in GitHub Desktop.
test
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Task: Add all the natural numbers below one thousand that are multiples of 3 or 5."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"source": [
"int Problem1_ForLoop(int size) {\n",
" int sum = 0;\n",
" for (int i = 0; i < size; i++)\n",
" {\n",
" if (i % 3 == 0 || i % 5 == 0)\n",
" {\n",
" sum += i;\n",
" }\n",
" }\n",
" return sum;\n",
"}\n",
"Problem1_ForLoop(1000)"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": "<div class=\"dni-plaintext\">233168</div>"
},
"execution_count": 1,
"metadata": {}
}
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"source": [
"int Problem1_LinqSum(int size) => Enumerable.Range(1, size).Where(o => o % 3 == 0 || o % 5 == 0).Sum();\n",
"Problem1_LinqSum(1000)"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": "<div class=\"dni-plaintext\">234168</div>"
},
"execution_count": 1,
"metadata": {}
}
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"source": [
"int Problem1_LinqAccumulator(int size) => Enumerable.Range(1, size)\n",
" .Aggregate(0, (total, nr) => nr % 3 == 0 || nr % 5 == 0 ? total + nr : total);\n",
"Problem1_LinqAccumulator(1000)"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": "<div class=\"dni-plaintext\">234168</div>"
},
"execution_count": 1,
"metadata": {}
}
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"source": [
"int ArithmeticProgression(int nr) => nr * (nr + 1) / 2;\n",
"int ArithmeticProgressionMultiple(int nr, int multiple) => \n",
" multiple * nr * (nr + 1) / 2;\n",
"int Problem1_ArithmeticProgression(int size) => \n",
" ArithmeticProgressionMultiple(size/3, 3) \n",
" + ArithmeticProgressionMultiple(size/5, 5) \n",
" - ArithmeticProgressionMultiple(size/15, 15);\n",
"Problem1_ArithmeticProgression(1000)"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": "<div class=\"dni-plaintext\">234168</div>"
},
"execution_count": 1,
"metadata": {}
}
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"language_info": {
"file_extension": ".cs",
"mimetype": "text/x-csharp",
"name": "C#",
"pygments_lexer": "csharp",
"version": "8.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment