do perfect and fix rational numbers

This commit is contained in:
Ben Harris 2021-11-11 18:13:22 -05:00
parent 8f1a5c6beb
commit d4d5f1e4d0
10 changed files with 385 additions and 30 deletions

View File

@ -93,6 +93,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HyperOptimizedTelemetry", "
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RationalNumbers", "csharp\rational-numbers\RationalNumbers.csproj", "{6273C0C2-FAFD-4DE5-829B-D700CED941EA}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PerfectNumbers", "csharp\perfect-numbers\PerfectNumbers.csproj", "{C424BB3E-AADC-42A0-889E-BE1C5A53C2F3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -279,6 +281,10 @@ Global
{6273C0C2-FAFD-4DE5-829B-D700CED941EA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6273C0C2-FAFD-4DE5-829B-D700CED941EA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6273C0C2-FAFD-4DE5-829B-D700CED941EA}.Release|Any CPU.Build.0 = Release|Any CPU
{C424BB3E-AADC-42A0-889E-BE1C5A53C2F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C424BB3E-AADC-42A0-889E-BE1C5A53C2F3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C424BB3E-AADC-42A0-889E-BE1C5A53C2F3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C424BB3E-AADC-42A0-889E-BE1C5A53C2F3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,141 @@
###############################
# Core EditorConfig Options #
###############################
; This file is for unifying the coding style for different editors and IDEs.
; More information at:
; https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference?view=vs-2017
; https://docs.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options?view=vs-2017
root = true
[*]
indent_style = space
[PerfectNumbers.cs]
indent_size = 4
###############################
# .NET Coding Conventions #
###############################
# Organize usings
dotnet_sort_system_directives_first = true
dotnet_separate_import_directive_groups = true
# this. preferences
dotnet_style_qualification_for_field = false:suggestion
dotnet_style_qualification_for_property = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_event = false:suggestion
# Language keywords vs BCL types preferences
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
dotnet_style_predefined_type_for_member_access = true:suggestion
# Parentheses preferences
dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_other_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:suggestion
# Modifier preferences
dotnet_style_require_accessibility_modifiers = always:suggestion
dotnet_style_readonly_field = true:suggestion
# Expression-level preferences
dotnet_style_object_initializer = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_explicit_tuple_names = true:suggestion
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_auto_properties = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion
dotnet_style_prefer_conditional_expression_over_return = true:suggestion
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_null_propagation = true:suggestion
###############################
# Naming Conventions #
###############################
# Style Definitions
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
# Use PascalCase for constant fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
dotnet_naming_symbols.constant_fields.applicable_kinds = field
dotnet_naming_symbols.constant_fields.applicable_accessibilities = *
dotnet_naming_symbols.constant_fields.required_modifiers = const
###############################
# C# Code Style Rules #
###############################
# var preferences
csharp_style_var_for_built_in_types = true:none
csharp_style_var_when_type_is_apparent = true:none
csharp_style_var_elsewhere = true:none
# Expression-bodied members
csharp_style_expression_bodied_methods = true:suggestion
csharp_style_expression_bodied_constructors = true:suggestion
csharp_style_expression_bodied_operators = true:suggestion
csharp_style_expression_bodied_properties = true:suggestion
csharp_style_expression_bodied_indexers = true:suggestion
csharp_style_expression_bodied_accessors = true:suggestion
# Pattern-matching preferences
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
# Null-checking preferences
csharp_style_throw_expression = true:suggestion
csharp_style_conditional_delegate_call = true:suggestion
# Modifier preferences
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion
# Expression-level preferences
csharp_prefer_braces = true:none
csharp_prefer_simple_default_expression = true:suggestion
csharp_style_deconstructed_variable_declaration = true:suggestion
csharp_style_pattern_local_over_anonymous_function = true:suggestion
csharp_style_inlined_variable_declaration = true:suggestion
###############################
# C# Formatting Rules #
###############################
# New line preferences
csharp_new_line_before_open_brace = all
csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_object_initializers = false
csharp_new_line_before_members_in_anonymous_types = false
csharp_new_line_between_query_expression_clauses = true
# Indentation preferences
csharp_indent_case_contents = true
csharp_indent_switch_labels = true
csharp_indent_labels = flush_left
# Space preferences
csharp_space_after_cast = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_around_binary_operators = before_and_after
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false
# Wrapping preferences
csharp_preserve_single_line_blocks = true
csharp_preserve_single_line_statements = true

View File

@ -0,0 +1,24 @@
{
"blurb": "Determine if a number is perfect, abundant, or deficient based on Nicomachus' (60 - 120 CE) classification scheme for positive integers.",
"authors": [
"robkeim"
],
"contributors": [
"ErikSchierboom",
"j2jensen",
"wolf99"
],
"files": {
"solution": [
"PerfectNumbers.cs"
],
"test": [
"PerfectNumbersTests.cs"
],
"example": [
".meta/Example.cs"
]
},
"source": "Taken from Chapter 2 of Functional Thinking by Neal Ford.",
"source_url": "http://shop.oreilly.com/product/0636920029687.do"
}

View File

@ -0,0 +1 @@
{"track":"csharp","exercise":"perfect-numbers","id":"09422b4aa7784b4e9af7f2458e33175f","url":"https://exercism.org/tracks/csharp/exercises/perfect-numbers","handle":"benharri","is_requester":true,"auto_approve":false}

View File

@ -0,0 +1,39 @@
# Help
## Running the tests
You can run the tests by opening a command prompt in the exercise's directory, and then running the [`dotnet test` command](https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-test)
Alternatively, most IDE's have built-in support for running tests, including [Visual Studio](https://docs.microsoft.com/en-us/visualstudio/test/run-unit-tests-with-test-explorer), [Rider](https://www.jetbrains.com/help/rider/Unit_Testing_in_Solution.html) and [Visual Studio code](https://github.com/OmniSharp/omnisharp-vscode/wiki/How-to-run-and-debug-unit-tests).
See the [tests page](https://exercism.io/tracks/csharp/tests) for more information.
## Skipped tests
Initially, only the first test will be enabled.
This is to encourage you to solve the exercise one step at a time.
Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing.
## Submitting your solution
You can submit your solution using the `exercism submit PerfectNumbers.cs` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [C# track's documentation](https://exercism.org/docs/tracks/csharp)
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
To get help if you're having trouble, you can use one of the following resources:
- [Gitter](https://gitter.im/exercism/xcsharp) is Exercism C# track's Gitter room; go here to get support and ask questions related to the C# track.
- [/r/csharp](https://www.reddit.com/r/csharp) is the C# subreddit.
- [StackOverflow](http://stackoverflow.com/questions/tagged/c%23) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.

View File

@ -0,0 +1,23 @@
using System;
using System.Linq;
public enum Classification
{
Perfect,
Abundant,
Deficient
}
public static class PerfectNumbers
{
public static Classification Classify(int i)
{
if (i < 1) throw new ArgumentOutOfRangeException(nameof(i));
var s = Enumerable.Range(1, (i + 1) / 2)
.Where(div => div != i && i % div == 0)
.Sum();
return s < i ? Classification.Deficient : s > i ? Classification.Abundant : Classification.Perfect;
}
}

View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<PackageReference Include="Exercism.Tests" Version="0.1.0-beta1" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,83 @@
using System;
using Xunit;
public class PerfectNumbersTests
{
[Fact]
public void Smallest_perfect_number_is_classified_correctly()
{
Assert.Equal(Classification.Perfect, PerfectNumbers.Classify(6));
}
[Fact]
public void Medium_perfect_number_is_classified_correctly()
{
Assert.Equal(Classification.Perfect, PerfectNumbers.Classify(28));
}
[Fact]
public void Large_perfect_number_is_classified_correctly()
{
Assert.Equal(Classification.Perfect, PerfectNumbers.Classify(33550336));
}
[Fact]
public void Smallest_abundant_number_is_classified_correctly()
{
Assert.Equal(Classification.Abundant, PerfectNumbers.Classify(12));
}
[Fact]
public void Medium_abundant_number_is_classified_correctly()
{
Assert.Equal(Classification.Abundant, PerfectNumbers.Classify(30));
}
[Fact]
public void Large_abundant_number_is_classified_correctly()
{
Assert.Equal(Classification.Abundant, PerfectNumbers.Classify(33550335));
}
[Fact]
public void Smallest_prime_deficient_number_is_classified_correctly()
{
Assert.Equal(Classification.Deficient, PerfectNumbers.Classify(2));
}
[Fact]
public void Smallest_non_prime_deficient_number_is_classified_correctly()
{
Assert.Equal(Classification.Deficient, PerfectNumbers.Classify(4));
}
[Fact]
public void Medium_deficient_number_is_classified_correctly()
{
Assert.Equal(Classification.Deficient, PerfectNumbers.Classify(32));
}
[Fact]
public void Large_deficient_number_is_classified_correctly()
{
Assert.Equal(Classification.Deficient, PerfectNumbers.Classify(33550337));
}
[Fact]
public void Edge_case_no_factors_other_than_itself_is_classified_correctly()
{
Assert.Equal(Classification.Deficient, PerfectNumbers.Classify(1));
}
[Fact]
public void Zero_is_rejected_as_it_is_not_a_positive_integer_()
{
Assert.Throws<ArgumentOutOfRangeException>(() => PerfectNumbers.Classify(0));
}
[Fact]
public void Negative_integer_is_rejected_as_it_is_not_a_positive_integer_()
{
Assert.Throws<ArgumentOutOfRangeException>(() => PerfectNumbers.Classify(-1));
}
}

View File

@ -0,0 +1,39 @@
# Perfect Numbers
Welcome to Perfect Numbers on Exercism's C# Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Determine if a number is perfect, abundant, or deficient based on
Nicomachus' (60 - 120 CE) classification scheme for positive integers.
The Greek mathematician [Nicomachus](https://en.wikipedia.org/wiki/Nicomachus) devised a classification scheme for positive integers, identifying each as belonging uniquely to the categories of **perfect**, **abundant**, or **deficient** based on their [aliquot sum](https://en.wikipedia.org/wiki/Aliquot_sum). The aliquot sum is defined as the sum of the factors of a number not including the number itself. For example, the aliquot sum of 15 is (1 + 3 + 5) = 9
- **Perfect**: aliquot sum = number
- 6 is a perfect number because (1 + 2 + 3) = 6
- 28 is a perfect number because (1 + 2 + 4 + 7 + 14) = 28
- **Abundant**: aliquot sum > number
- 12 is an abundant number because (1 + 2 + 3 + 4 + 6) = 16
- 24 is an abundant number because (1 + 2 + 3 + 4 + 6 + 8 + 12) = 36
- **Deficient**: aliquot sum < number
- 8 is a deficient number because (1 + 2 + 4) = 7
- Prime numbers are deficient
Implement a way to determine whether a given number is **perfect**. Depending on your language track, you may also need to implement a way to determine whether a given number is **abundant** or **deficient**.
## Source
### Created by
- @robkeim
### Contributed to by
- @ErikSchierboom
- @j2jensen
- @wolf99
### Based on
Taken from Chapter 2 of Functional Thinking by Neal Ford. - http://shop.oreilly.com/product/0636920029687.do

View File

@ -2,59 +2,44 @@
public static class RealNumberExtension
{
public static double Expreal(this int realNumber, RationalNumber r) =>
Math.Pow(realNumber, r.AsDouble());
public static double Expreal(this int n, RationalNumber r) => Math.Pow(n, r);
}
public struct RationalNumber
{
private int _numerator;
private int _denominator;
private readonly int num;
private readonly int den;
public RationalNumber(int numerator, int denominator)
{
_numerator = numerator;
_denominator = denominator;
if (_denominator < 0)
{
_numerator = -_numerator;
_denominator = -_denominator;
}
var gcd = GreatestCommonDivisor(numerator, denominator);
num = numerator / gcd;
den = denominator / gcd;
}
public override string ToString() => $"{_numerator} / {_denominator}";
public static RationalNumber operator +(RationalNumber r1, RationalNumber r2) =>
new(r1._numerator * r2._denominator + r2._numerator * r1._denominator, r1._denominator * r2._denominator);
new((r1.num * r2.den) + (r2.num * r1.den), r1.den * r2.den);
public static RationalNumber operator -(RationalNumber r1, RationalNumber r2) =>
r1 with { _numerator = -r1._numerator } + r2;
new((r1.num * r2.den) - (r2.num * r1.den), r1.den * r2.den);
public static RationalNumber operator *(RationalNumber r1, RationalNumber r2) =>
new(r1._numerator * r2._numerator, r1._denominator * r2._denominator);
new(r1.num * r2.num, r1.den * r2.den);
public static RationalNumber operator /(RationalNumber r1, RationalNumber r2) =>
new(r1._numerator * r2._denominator, r1._denominator * r2._numerator);
new(r1.num * r2.den, r1.den * r2.num);
public RationalNumber Abs() =>
new(Math.Abs(_numerator), Math.Abs(_denominator));
new(Math.Abs(num), Math.Abs(den));
public RationalNumber Reduce()
{
var gcd = GreatestCommonDivisor(_numerator, _denominator);
return new(_numerator / gcd, _denominator / gcd);
}
public RationalNumber Reduce() => this;
public RationalNumber Exprational(int power) =>
new((int)Math.Pow(_numerator, power), (int)Math.Pow(_denominator, power));
new((int)Math.Pow(num, power), (int)Math.Pow(den, power));
public double Expreal(int baseNumber) =>
Math.Pow(Math.Pow(baseNumber, _numerator), 1d / _denominator);
public static int GreatestCommonDivisor(int a, int b) =>
b == 0 ? a : GreatestCommonDivisor(b, a % b);
public double AsDouble() =>
_numerator / (double)_denominator;
public static implicit operator double(RationalNumber r) =>
(double) r.num / r.den;
}