Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project euler 24 #1730

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
b7309f0
📦 NEW: Added solution for ProjectEuler-007
omkarnathparida Oct 7, 2022
d3a3b33
🐛 FIX: Spelling mistake fixes
omkarnathparida Oct 7, 2022
e9b5a6a
👌 IMPROVE: changed variable name from `inc` to `candidateValue` and t…
omkarnathparida Oct 7, 2022
e99c722
👌 IMPROVE: Modified the code
omkarnathparida Oct 7, 2022
5ac898f
Merge branch 'TheAlgorithms:master' into master
pomkarnath98 Oct 10, 2022
0f9f1ba
👌 IMPROVE: Added test case for ProjectEuler Problem001
omkarnathparida Oct 10, 2022
3caad5c
Merge branch 'TheAlgorithms:master' into master
pomkarnath98 Oct 15, 2022
b7584fd
Merge branch 'TheAlgorithms:master' into master
pomkarnath98 Oct 16, 2022
6693f9c
Merge branch 'TheAlgorithms:master' into master
pomkarnath98 Oct 17, 2022
0830570
Merge branch 'TheAlgorithms:master' into master
pomkarnath98 Oct 18, 2022
4d7149c
Merge branch 'TheAlgorithms:master' into master
pomkarnath98 Oct 22, 2023
53e3938
👌 IMPROVE: Added test cases for Project Euler Problem 4
omkarnathparida Oct 22, 2023
96224e7
👌 IMPROVE: auto prettier fixes
omkarnathparida Oct 22, 2023
718d515
Merge branch 'TheAlgorithms:master' into master
pomkarnath98 Sep 28, 2024
396782d
Merge branch 'TheAlgorithms:master' into master
pomkarnath98 Oct 5, 2024
944077b
Merge branch 'TheAlgorithms:master' into master
pomkarnath98 Oct 8, 2024
8d15caf
Merge branch 'TheAlgorithms:master' into master
pomkarnath98 Oct 9, 2024
31aca33
📦 NEW: Project Euler 22 | Added solution and test cases
omkarnathparida Oct 9, 2024
e7e5716
📦 NEW: Added Solution and Testcases for Project Euler Problem 24
pomkarnath98 Oct 13, 2024
9fde20a
Updated Documentation in README.md
pomkarnath98 Oct 13, 2024
0eb0a9b
👌 IMPROVE: Added Problem Statement
pomkarnath98 Oct 13, 2024
f06a49e
Merge branch 'project-euler-24' of https://github.com/pomkarnath98/Ja…
pomkarnath98 Oct 13, 2024
ca7a5e8
🐛 FIX: code improvements
pomkarnath98 Oct 13, 2024
bb0b2d6
🐛 FIX: code improvement
pomkarnath98 Oct 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,10 @@
* [Problem019](Project-Euler/Problem019.js)
* [Problem020](Project-Euler/Problem020.js)
* [Problem021](Project-Euler/Problem021.js)
* **Problem022**
* [Problem022](Project-Euler/Problem022/Problem022.js)
* [Problem023](Project-Euler/Problem023.js)
* [Problem024](Project-Euler/Problem024.js)
* [Problem025](Project-Euler/Problem025.js)
* [Problem028](Project-Euler/Problem028.js)
* [Problem035](Project-Euler/Problem035.js)
Expand Down
38 changes: 38 additions & 0 deletions Project-Euler/Problem022/Names.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"MARY","PATRICIA","LINDA","BARBARA","ELIZABETH","JENNIFER","MARIA","SUSAN","MARGARET","DOROTHY",
"LISA","NANCY","KAREN","BETTY","HELEN","SANDRA","DONNA","CAROL","RUTH","SHARON","MICHELLE",
"LAURA","SARAH","KIMBERLY","DEBORAH","JESSICA","SHIRLEY","CYNTHIA","ANGELA","MELISSA","BRENDA",
"AMY","ANNA","REBECCA","VIRGINIA","KATHLEEN","PAMELA","MARTHA","DEBRA","AMANDA","STEPHANIE",
"CAROLYN","CHRISTINE","MARIE","JANET","CATHERINE","FRANCES","ANN","JOYCE","DIANE","ALICE",
"JULIE","HEATHER","TERESA","DORIS","GLORIA","EVELYN","JEAN","CHERYL","MILDRED","KATHERINE",
"JOAN","ASHLEY","JUDITH","ROSE","JANICE","KELLY","NICOLE","JUDY","CHRISTINA","KATHY",
"THERESA","BEVERLY","DENISE","TAMMY","IRENE","JANE","LORI","RACHEL","MARILYN","ANDREA",
"KATHRYN","LOUISE","SARA","ANNE","JACQUELINE","WANDA","BONNIE","JULIA","RUBY","LOIS",
"TINA","PHYLLIS","NORMA","PAULA","DIANA","ANNIE","LILLIAN","EMILY","ROBIN","PEGGY",
"CRYSTAL","GLADYS","RITA","DAWN","CONNIE","FLORENCE","TRACY","EDNA","TIFFANY","CARMEN",
"ROSA","CINDY","GRACE","WENDY","VICTORIA","EDITH","KIM","SHERRY","SYLVIA","JOSEPHINE",
"THELMA","SHANNON","SHEILA","ETHEL","ELLEN","ELAINE","MARJORIE","CARRIE","CHARLOTTE","MONICA",
"ESTHER","PAULINE","EMMA","JUANITA","ANITA","RHONDA","HAZEL","AMBER","EVA","DEBBIE",
"APRIL","LESLIE","CLARA","LUCILLE","JAMIE","JOANNE","ELEANOR","VALERIE","DANIELLE","MEGAN",
"ALICIA","SUZANNE","MICHELE","GAIL","BERTHA","DARLENE","VERONICA","JILL","ERIN","GERALDINE",
"LAUREN","CATHY","JOANN","LORRAINE","LYNN","SALLY","REGINA","ERICA","BEATRICE","DOLORES",
"BERNICE","AUDREY","YVONNE","ANNETTE","JUNE","SAMANTHA","MARION","DANA","STACY","ANA",
"RENEE","IDA","VIVIAN","ROBERTA","HOLLY","BRITTANY","MELANIE","LORETTA","YOLANDA","JEANETTE",
"LAURIE","KATIE","KRISTEN","VANESSA","ALMA","SUE","ELSIE","BETH","JEANNE","VICKI",
"CARLA","TARA","ROSEMARY","EILEEN","TERRI","GERTRUDE","LUCY","TONYA","ELLA","STACEY",
"WILMA","GINA","KRISTIN","JESSIE","NATALIE","AGNES","VERA","WILLIE","CHARLENE","BESSIE",
"DELORES","MELINDA","PEARL","ARLENE","MAUREEN","COLLEEN","ALLISON","TAMARA","JOY","GEORGIA",
"CONSTANCE","LILLIE","CLAUDIA","JACKIE","MARCIA","TANYA","NELLIE","MINNIE","MARLENE","HEIDI",
"GLENDA","LYDIA","VIOLA","COURTNEY","MARIAN","STELLA","CAROLINE","DORA","JO","VICKIE",
"MATTIE","TERRY","MAXINE","IRMA","MABEL","MARSHA","MYRTLE","LENA","CHRISTY","DEANNA",
"PATSY","HILDA","GWENDOLYN","JENNIE","NORA","MARGIE","NINA","CASSANDRA","LEAH","PENNY",
"KAY","PRISCILLA","NAOMI","CAROLE","BRANDY","OLGA","BILLIE","DIANNE","TRACEY","LEONA",
"JENNY","FELICIA","SONIA","MIRIAM","VELMA","BECKY","BOBBIE","VIOLET","KRISTINA","TONI",
"MISTY","MAE","SHELLY","DAISY","RAMONA","SHERRI","ERIKA","KATRINA","CLAIRE","LINDSEY",
"LINDSAY","GENEVA","GUADALUPE","BELINDA","MARGARITA","SHERYL","CORA","FAYE","ADA","NATASHA",
"SABRINA","ISABEL","MARGUERITE","HATTIE","HARRIET","MOLLY","CECILIA","KRISTI","BRANDI","BLANCHE",
"SANDY","ROSIE","JOANNA","IRIS","EUNICE","ANGIE","INEZ","LYNDA","MADELINE","AMELIA",
"ALYSSA","KRISTY","CATHLEEN","ANGELINE","FRIEDA","ANTOINETTE","CECIL","JASMINE","NORMA","HANNAH",
"SANDRA","KEISHA","CLAIRE","ELLA","TAMIKA","FANNIE","CELIA","PEGGY","MICHAELA","ALYSON",
"ALTHEA","KENDRA","LORA","CORA","SHIRLEY","MATTIE","EMMA","MONICA","CATHY","ELEANOR",
"REBECCA","TERESA","CLARA","PAMELA","SYLVIA","NANCY","FLORENCE","VICTORIA","ANITA","ELIZABETH",
"JESSIE","LORRAINE","GERTRUDE","KATHERINE","VIVIAN","LOUISE","SUSAN","WANDA","PATRICIA","MARY","OMKARNATH"
31 changes: 31 additions & 0 deletions Project-Euler/Problem022/Problem022.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Names Scores

Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.
For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 * 53 = 49714.
What is the total of all the name scores in the file?
*/
import fs from 'fs'

export const getAlphabeticalValue = (name) => {
return [...name].reduce(
(sum, char) => sum + (char.charCodeAt(0) - 'A'.charCodeAt(0) + 1),
0
)
}

export const calculateTotalNameScore = (filePath) => {
return new Promise((resolve) => {
fs.readFile(filePath, 'utf8', (_, data) => {
const names = data.replace(/"/g, '').split(',').sort()

const totalScore = names.reduce((total, name, index) => {
const alphabeticalValue = getAlphabeticalValue(name)
const nameScore = alphabeticalValue * (index + 1)
return total + nameScore
}, 0)

resolve(totalScore)
})
})
}
33 changes: 33 additions & 0 deletions Project-Euler/Problem022/Problem022.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { describe, it, expect } from 'vitest'
import { calculateTotalNameScore, getAlphabeticalValue } from './Problem022'
const path = require('path')

describe('getAlphabeticalValue', () => {
it('should return correct alphabetical value for COLIN', () => {
const name = 'COLIN'
const expectedValue = 3 + 15 + 12 + 9 + 14
expect(getAlphabeticalValue(name)).toBe(expectedValue)
})

it('should return correct alphabetical value for A', () => {
const name = 'A'
const expectedValue = 1
expect(getAlphabeticalValue(name)).toBe(expectedValue)
})

it('should return correct alphabetical value for Z', () => {
const name = 'Z'
const expectedValue = 26
expect(getAlphabeticalValue(name)).toBe(expectedValue)
})
})

describe('calculateTotalNameScore', () => {
it('should correctly calculate the total name score', async () => {
const namesFilePath = path.join(__dirname, 'Names.txt')
const result = await calculateTotalNameScore(namesFilePath)

const expectedScore = 4654143
expect(result).toBe(expectedScore)
})
})
29 changes: 29 additions & 0 deletions Project-Euler/Problem024.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:

012 021 102 120 201 210

What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
*/

function factorial(n) {
return n <= 1 ? 1 : n * factorial(n - 1)
}

export function findNthLexicographicPermutation(digits, n) {
let permutation = ''
let k = n - 1

while (digits.length > 0) {
const fact = factorial(digits.length - 1)
const index = Math.floor(k / fact)
permutation += digits[index]
digits.splice(index, 1)
k %= fact
}

return permutation
}

const digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
console.log(findNthLexicographicPermutation(digits, 1000000))
31 changes: 31 additions & 0 deletions Project-Euler/test/Problem024.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { describe, it, expect } from 'vitest'
import { findNthLexicographicPermutation } from '../Problem024'

describe('findNthLexicographicPermutation', () => {
it('should return the correct permutation for small cases', () => {
expect(findNthLexicographicPermutation([0, 1, 2], 1)).toBe('012')
expect(findNthLexicographicPermutation([0, 1, 2], 2)).toBe('021')
expect(findNthLexicographicPermutation([0, 1, 2], 6)).toBe('210')
})

it('should return the correct 1st permutation for digits 0-9', () => {
expect(
findNthLexicographicPermutation([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 1)
).toBe('0123456789')
})

it('should return the correct millionth permutation for digits 0-9', () => {
expect(
findNthLexicographicPermutation([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 1000000)
).toBe('2783915460')
})

it('should return the correct permutation for a smaller set of digits', () => {
expect(findNthLexicographicPermutation([1, 2, 3, 4], 12)).toBe('2431')
expect(findNthLexicographicPermutation([1, 2, 3, 4], 24)).toBe('4321')
})

it('should handle large values of n', () => {
expect(findNthLexicographicPermutation([0, 1, 2, 3, 4], 120)).toBe('43210')
})
})