4 kyu

Sum by Factors

4,615 of 20,052g964

Description:

Given an array of positive or negative integers

I= [i1,..,in]

you have to produce a sorted array P of the form

[ [p, sum of all ij of I for which p is a prime factor (p positive) of ij] ...]

P will be sorted by increasing order of the prime numbers. The final result has to be given as a string in Java, C#, C, C++ and as an array of arrays in other languages.

Example:

I = (/12, 15/); // result = "(2 12)(3 27)(5 15)"
I = [12, 15] # result = [[2, 12], [3, 27], [5, 15]]
I = [12, 15] # result = [{2, 12}, {3, 27}, {5, 15}]
I = [12, 15] # result = [(2, 12), (3, 27), (5, 15)]
I = [12, 15] # result = [(2, 12), (3, 27), (5, 15)]
I = [12, 15] # result = [[2, 12], [3, 27], [5, 15]]
I = {12, 15}; // result = "(2 12)(3 27)(5 15)"
I = {12, 15}; // result = "(2 12)(3 27)(5 15)"
I = {12, 15}; // result = "(2 12)(3 27)(5 15)"
I = {12, 15}; // result = "(2 12)(3 27)(5 15)"
I = [12, 15] ; result = [[2, 12], [3, 27], [5, 15]]
I = [12, 15] -- result = [(2,12),(3,27),(5,15)]
I = [12, 15]; //result = [[2, 12], [3, 27], [5, 15]]
I = [12, 15] # result = [[2, 12], [3, 27], [5, 15]]
I = [12, 15]; //result = [[2, 12], [3, 27], [5, 15]]
I = [12, 15]; //result = [[2, 12], [3, 27], [5, 15]]
       I = [12, 15]
      * result = [[2, 12], [3, 27], [5, 15]]

[2, 3, 5] is the list of all prime factors of the elements of I, hence the result.

Notes:

  • It can happen that a sum is 0 if some numbers are negative!

Example: I = [15, 30, -45] 5 divides 15, 30 and (-45) so 5 appears in the result, the sum of the numbers for which 5 is a factor is 0 so we have [5, 0] in the result amongst others.

  • In Fortran - as in any other language - the returned string is not permitted to contain any redundant trailing whitespace: you can use dynamically allocated character strings.
Arrays
Algorithms
Mathematics

More By Author:

Check out these other kata created by g964

Stats:

CreatedFeb 6, 2015
PublishedFeb 6, 2015
Warriors Trained78343
Total Skips24396
Total Code Submissions187599
Total Times Completed20052
Java Completions2278
Python Completions6863
C# Completions1212
Ruby Completions770
Clojure Completions100
Haskell Completions191
JavaScript Completions4615
CoffeeScript Completions38
C++ Completions1223
TypeScript Completions456
Elixir Completions75
PHP Completions322
Crystal Completions18
C Completions556
Rust Completions526
Swift Completions178
F# Completions51
R Completions76
Shell Completions36
OCaml Completions31
Kotlin Completions228
Fortran Completions10
Groovy Completions13
Julia Completions38
Scala Completions125
PowerShell Completions21
Go Completions399
Nim Completions12
Reason Completions3
Racket Completions20
VB Completions14
Haxe Completions12
CommonLisp Completions54
Dart Completions105
Lua Completions48
Pascal Completions9
Perl Completions16
Elm Completions4
COBOL Completions7
D Completions7
Erlang Completions7
Prolog Completions9
Total Stars1688
% of votes with a positive feedback rating89% of 2338
Total "Very Satisfied" Votes1892
Total "Somewhat Satisfied" Votes376
Total "Not Satisfied" Votes70
Ad
Contributors
  • g964 Avatar
  • jhoffner Avatar
  • kazk Avatar
  • Blind4Basics Avatar
  • Voile Avatar
  • ice1000 Avatar
  • monadius Avatar
  • user9644768 Avatar
  • hobovsky Avatar
  • trashy_incel Avatar
  • user8436785 Avatar
  • akar-0 Avatar
  • Just4FunCoder Avatar
  • 4500zenja1 Avatar
  • saudiGuy Avatar
Ad