using System.Collections.Generic; using System.Linq; //using FactorSort = (int PrimeFactor, int Exponent); public static class PrimeFactorization { public static IList<(int PrimeFactor, int Exponent)> Factorize(int number) { List<(int PrimeFactor, int Exponent)> factors = new() { DetermineFrequency(ref number, 2) }; for (int i = 3; i*i <= number; i += 2) factors.Add(DetermineFrequency(ref number, i)); if (number > 1) factors.Add((number, 1)); factors.RemoveAll(f => f.Exponent == 0); return factors; } private static (int PrimeFactor, int Exponent) DetermineFrequency(ref int number, int factor) { var exponent = 0; while (number % factor == 0) { exponent++; number /= factor; } return (factor, exponent); } }
- using System.Collections.Generic;
- using System.Linq;
- //using FactorSort = (int PrimeFactor, int Exponent);
- public static class PrimeFactorization
- {
public static IEnumerable<(int PrimeFactor, int Exponent)> Factorize(int number)- public static IList<(int PrimeFactor, int Exponent)> Factorize(int number)
- {
- List<(int PrimeFactor, int Exponent)> factors = new()
- {
- DetermineFrequency(ref number, 2)
- };
- for (int i = 3; i*i <= number; i += 2)
- factors.Add(DetermineFrequency(ref number, i));
- if (number > 1) factors.Add((number, 1));
- factors.RemoveAll(f => f.Exponent == 0);
- return factors;
- }
- private static (int PrimeFactor, int Exponent) DetermineFrequency(ref int number, int factor)
- {
SortedDictionary<int, int> factors = new(); //[];for (int i = 2; i <= number / 2 + 1; i++)- var exponent = 0;
- while (number % factor == 0)
- {
while (number % i == 0){factors[i] = factors.GetValueOrDefault(i, 0) + 1;number /= i;}- exponent++;
- number /= factor;
- }
if (number > 1) factors[number] = factors.GetValueOrDefault(number, 0) + 1;return from factor in factors select (factor.Key, factor.Value);- return (factor, exponent);
- }
- }
using NUnit.Framework; using System.Collections.Generic; namespace Testing; [TestFixture] public class PrimeFactorizationTest { private List<(int input, List<(int PrimeFactor, int Exponent)> expectedOutput)> _testCases = new() { (84, new() { (2, 2), (3, 1), (7, 1) }), (100, new() { (2, 2), (5, 2) }), (79, new() { (79, 1) }), (13195, new() { (5, 1), (7, 1), (13, 1), (29, 1) }), (30030, new() { (2, 1), (3, 1), (5, 1), (7, 1), (11, 1), (13, 1) }) }; [Test] public void Test() { foreach (var testCase in _testCases) { Assert.That(PrimeFactorization.Factorize(testCase.input), Is.EqualTo(testCase.expectedOutput)); } } }
- using NUnit.Framework;
- using System.Collections.Generic;
- namespace Testing;
- [TestFixture]
- public class PrimeFactorizationTest
- {
- private List<(int input, List<(int PrimeFactor, int Exponent)> expectedOutput)> _testCases = new()
- {
- (84, new() { (2, 2), (3, 1), (7, 1) }),
- (100, new() { (2, 2), (5, 2) }),
- (79, new() { (79, 1) }),
- (13195, new() { (5, 1), (7, 1), (13, 1), (29, 1) }),
- (30030, new() { (2, 1), (3, 1), (5, 1), (7, 1), (11, 1), (13, 1) })
- };
// [// (84, [(2, 2), (3, 1), (7, 1)]),// (100, [(2, 2), (5, 2)]),// (79, [(79, 1)]),// (13195, [(5, 1), (7, 1), (13, 1), (29, 1)]),// (30030, [(2, 1), (3, 1), (5, 1), (7, 1), (11, 1), (13, 1)])// ];- [Test]
- public void Test()
- {
- foreach (var testCase in _testCases)
- {
- Assert.That(PrimeFactorization.Factorize(testCase.input), Is.EqualTo(testCase.expectedOutput));
- }
- }
- }
using System.Collections.Generic; using System.Linq; public static class PrimeFactorization { public static IEnumerable<(int PrimeFactor, int Exponent)> Factorize(int number) { SortedDictionary<int, int> factors = new(); //[]; for (int i = 2; i <= number / 2 + 1; i++) { while (number % i == 0) { factors[i] = factors.GetValueOrDefault(i, 0) + 1; number /= i; } } if (number > 1) factors[number] = factors.GetValueOrDefault(number, 0) + 1; return from factor in factors select (factor.Key, factor.Value); } }
def prime_factorization(input_number):factors = []for i in range(2, int(input_number**0.5) + 1):while input_number % i == 0:factors.append(i)input_number //= iif input_number > 1:factors.append(input_number)from collections import Counterreturn sorted(Counter(factors).items())- using System.Collections.Generic;
- using System.Linq;
- public static class PrimeFactorization
- {
- public static IEnumerable<(int PrimeFactor, int Exponent)> Factorize(int number)
- {
- SortedDictionary<int, int> factors = new(); //[];
- for (int i = 2; i <= number / 2 + 1; i++)
- {
- while (number % i == 0)
- {
- factors[i] = factors.GetValueOrDefault(i, 0) + 1;
- number /= i;
- }
- }
- if (number > 1) factors[number] = factors.GetValueOrDefault(number, 0) + 1;
- return from factor in factors select (factor.Key, factor.Value);
- }
- }
using NUnit.Framework; using System.Collections.Generic; namespace Testing; [TestFixture] public class PrimeFactorizationTest { private List<(int input, List<(int PrimeFactor, int Exponent)> expectedOutput)> _testCases = new() { (84, new() { (2, 2), (3, 1), (7, 1) }), (100, new() { (2, 2), (5, 2) }), (79, new() { (79, 1) }), (13195, new() { (5, 1), (7, 1), (13, 1), (29, 1) }), (30030, new() { (2, 1), (3, 1), (5, 1), (7, 1), (11, 1), (13, 1) }) }; // [ // (84, [(2, 2), (3, 1), (7, 1)]), // (100, [(2, 2), (5, 2)]), // (79, [(79, 1)]), // (13195, [(5, 1), (7, 1), (13, 1), (29, 1)]), // (30030, [(2, 1), (3, 1), (5, 1), (7, 1), (11, 1), (13, 1)]) // ]; [Test] public void Test() { foreach (var testCase in _testCases) { Assert.That(PrimeFactorization.Factorize(testCase.input), Is.EqualTo(testCase.expectedOutput)); } } }
import codewars_test as testimport solution # Assuming the solution module contains the function prime_factorization- using NUnit.Framework;
- using System.Collections.Generic;
@test.describe("Prime Factorization Tests")def test_group():@test.it("Test with number 84")def test_case():input_number = 84expected_output = [(2, 2), (3, 1), (7, 1)]test.assert_equals(solution.prime_factorization(input_number), expected_output)- namespace Testing;
@test.it("Test with number 100")def test_case():input_number = 100expected_output = [(2, 2), (5, 2)]test.assert_equals(solution.prime_factorization(input_number), expected_output)- [TestFixture]
- public class PrimeFactorizationTest
- {
- private List<(int input, List<(int PrimeFactor, int Exponent)> expectedOutput)> _testCases = new()
- {
- (84, new() { (2, 2), (3, 1), (7, 1) }),
- (100, new() { (2, 2), (5, 2) }),
- (79, new() { (79, 1) }),
- (13195, new() { (5, 1), (7, 1), (13, 1), (29, 1) }),
- (30030, new() { (2, 1), (3, 1), (5, 1), (7, 1), (11, 1), (13, 1) })
- };
- // [
- // (84, [(2, 2), (3, 1), (7, 1)]),
- // (100, [(2, 2), (5, 2)]),
- // (79, [(79, 1)]),
- // (13195, [(5, 1), (7, 1), (13, 1), (29, 1)]),
- // (30030, [(2, 1), (3, 1), (5, 1), (7, 1), (11, 1), (13, 1)])
- // ];
@test.it("Test with number 79")def test_case():input_number = 79expected_output = [(79, 1)]test.assert_equals(solution.prime_factorization(input_number), expected_output)@test.it("Test with number 13195")def test_case():input_number = 13195expected_output = [(5, 1), (7, 1), (13, 1), (29, 1)]test.assert_equals(solution.prime_factorization(input_number), expected_output)@test.it("Test with number 30030")def test_case():input_number = 30030expected_output = [(2, 1), (3, 1), (5, 1), (7, 1), (11, 1), (13, 1)]test.assert_equals(solution.prime_factorization(input_number), expected_output)- [Test]
- public void Test()
- {
- foreach (var testCase in _testCases)
- {
- Assert.That(PrimeFactorization.Factorize(testCase.input), Is.EqualTo(testCase.expectedOutput));
- }
- }
- }