-
Notifications
You must be signed in to change notification settings - Fork 30
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
Generating Sequences #239
Open
MicahWoldu
wants to merge
3
commits into
master
Choose a base branch
from
generating_sequences
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Generating Sequences #239
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Generating-sequences | ||
|
||
Mathematical sequences occur so frequently in nature very often this transfers into computer programming problems. For example, the | ||
cicadas who appear periodically but only emerge after a prime number of years. Or sunflowers which have either 34, 55 or 89 petals | ||
on their face, depending upon the size of the sunflower. Additionally, lilies and irises have three petals, buttercups and wild roses | ||
have five, delphiniums have eight petals, all related to the Fibonacci sequence. Or simply triangular numbers in bowling! | ||
We are surrounded by sequences which are regularly analysed within computer programming. | ||
|
||
Being equipped to generate such sequences is vital. This article is dedicated to exploring the possible ways these sequences can be produced | ||
within a computer program using javascript: | ||
|
||
Prime Numbers | ||
|
||
A prime number is a natural number that has exactly two distinct natural number divisors: 1 and itself. There are several prime number | ||
sieves in circulation, however, Sieve of Eratosthenes is a simple ancient algorithm. | ||
|
||
By iterating through an array of numbers starting at the beginning and removing all subsequent numbers that are a factor of the prime. | ||
The multiples of a given prime are generated as a sequence of numbers starting from that prime, with constant difference between | ||
them that is equal to that prime. This is the sieve's key distinction from using trial division to sequentially test each | ||
candidate number for divisibility by each prime: | ||
|
||
function primes(num) { | ||
var x = []; | ||
for( var i =0; i<= num; i++) { | ||
x[i] = true; | ||
} | ||
x[0]= false; | ||
x[1]= false; | ||
x[2]= true; | ||
for ( var j = 2; j< num; j++) { | ||
for (var k = 2; j*k <= num; k++) { | ||
x[j*k] = false; | ||
} | ||
} | ||
|
||
primeValues = []; | ||
|
||
for (var i = 0; i< num; i ++) { | ||
if (x[i] == true) { | ||
primeValues.push(i); | ||
} | ||
} | ||
return primeValues; | ||
} | ||
|
||
primes(20); | ||
[2, 3, 5, 7, 11, 13, 17, 19] | ||
|
||
Fibonnaci Sequence | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps make this heading bold/bigger to improve readability |
||
|
||
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation Fn = Fn-1 + Fn-2 with seed values 0,1. | ||
|
||
function fibonacci(nums) { | ||
|
||
var fibSequence = []; | ||
|
||
fibSequence[0] = 0; | ||
fibSequence[1] = 1; | ||
|
||
for (var i = 2; i < nums; i ++) { | ||
fibSequence[i] = fibSequence[i-1] + fibSequence[i-2]; | ||
} | ||
|
||
return fibSequence; | ||
} | ||
|
||
fibonacci(8); | ||
[ 0, 1, 1, 2, 3, 5, 8, 13 ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Under-the-Hood | ||
|
||
Fundamentally all characters are a higher level abstraction of a bit number. Subsequently every character has a corresponding number value. Many programming problems are made more accessible when viewed in this way. Furthermore, it also helps to lend understanding to built-in Javascript methods such as sort(); which is able to arrange characters of the same case in alphabetical order. | ||
|
||
Example of sort() being used: | ||
|
||
function arrange(values) { | ||
values.sort(); | ||
return values; | ||
} | ||
arrange(["China", "Germany", "United States", "Great Britain", Brazil"]); | ||
|
||
//[ 'Brazil', 'China', 'Germany', 'Great Britain', 'United States' ] | ||
|
||
In order to discover the unicode value of a particular character at a specified index in a string the charCodeAt() method is used: | ||
|
||
function findCharCode(string) { | ||
var newArray = []; | ||
for(var i = 0; i < string.length; i++) { | ||
newArray.push(string.charCodeAt(i)); | ||
} | ||
return newArray; | ||
} | ||
findCharCode("GITHUB"); | ||
|
||
//[71, 73, 84, 72, 85, 66] | ||
|
||
In contrast, the fromCharCode() method converts Unicode values into characters. Note: This is a static method of the String object, | ||
and the syntax is always String.fromCharCode(). | ||
|
||
function char(value) { | ||
var result = ""; | ||
for (var i = 0; i < value.length; i++) { | ||
result += String.fromCharCode(value[i]); | ||
} | ||
return result; | ||
} | ||
char([97, 121, 108, 101, 114]); | ||
|
||
//'ayler' | ||
|
||
Unicode Characters: | ||
|
||
D.V Char D.V Char D.V Char D.V Char | ||
32 SPACE 56 8 80 P 104 h | ||
33 ! 57 9 81 Q 105 i | ||
34 " 58 : 82 R 106 j | ||
35 # 59 ; 83 S 107 k | ||
36 $ 60 < 84 T 108 l | ||
37 % 61 = 85 U 109 m | ||
38 & 62 > 86 V 110 n | ||
39 ' 63 ? 87 W 111 o | ||
40 ( 64 @ 88 X 112 p | ||
41 ) 65 A 89 Y 113 q | ||
42 * 66 B 90 Z 114 r | ||
43 + 67 C 91 [ 115 s | ||
44 , 68 D 92 \ 116 t | ||
45 - 69 E 93 ] 117 u | ||
46 . 70 F 94 ^ 118 v | ||
47 / 71 G 95 _ 119 w | ||
48 0 72 H 96 ` 120 x | ||
49 1 73 I 97 a 121 y | ||
50 2 74 J 98 b 122 z | ||
51 3 75 K 99 c | ||
52 4 76 L 100 d | ||
53 5 77 M 101 e | ||
54 6 78 N 102 f | ||
55 7 79 O 103 g | ||
|
||
Key: D.V = Decimal Value |
Submodule READMEs
added at
305717
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps make this heading bold/bigger to improve readability