-
Notifications
You must be signed in to change notification settings - Fork 0
Binary representation #10
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
Open
kamendov-maxim
wants to merge
23
commits into
main
Choose a base branch
from
binaryRepresentation
base: main
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
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
12a2b6e
add fibonacci.c
kamendov-maxim 6026da5
add sortings.c
kamendov-maxim 5bcddbe
add sortings file
kamendov-maxim 6abef45
1
kamendov-maxim f2d1838
create the file
kamendov-maxim 0900edc
-
kamendov-maxim d2aab5e
-
kamendov-maxim 69e13dd
-
kamendov-maxim 553c768
change directory
kamendov-maxim 5bcf83a
tests1
kamendov-maxim a9c5988
-
kamendov-maxim 72f341c
-
kamendov-maxim d6b2f9d
-
kamendov-maxim 122a430
-
kamendov-maxim bef8a09
-
kamendov-maxim 8e89026
-
kamendov-maxim 84899fa
-
kamendov-maxim 9beb0fd
search
kamendov-maxim 956313d
mostFrequentElement task
kamendov-maxim 1b6093d
mostFrequentElement2
kamendov-maxim 3ebfb91
decimalTOBinary function
kamendov-maxim d409fac
Двоичное представление
kamendov-maxim afb3eb5
-
kamendov-maxim 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,91 +1,183 @@ | ||
| #include <stdio.h> | ||
| #include <stdbool.h> | ||
| #include <time.h> | ||
|
|
||
| bool checkIfNumberIsCorrect(int n) | ||
| int iterativeFibonacci(int number, long long int *answer); | ||
| int recursiveFibonacci(int number, long long int *answer); | ||
| long long int recursiveFibonacciBody(int number); | ||
| bool correctInputCheckForRecursiveFunction(int number); | ||
| bool testIterativeFunction(void); | ||
| bool testRecursiveFunction(void); | ||
| bool test(void); | ||
|
|
||
| int main() | ||
| { | ||
| if (n < 1) | ||
|
|
||
| if (!test()) | ||
| { | ||
| return false; | ||
| printf("\nSorry but the program does not work correctly\n"); | ||
| return 1; | ||
| } | ||
| return true; | ||
|
|
||
| printf("\nEnter the number of fbonacci number you want to see: "); | ||
| int number = 0; | ||
| int scanfNumberOfElements = scanf("%d", &number); | ||
|
|
||
| if (scanfNumberOfElements != 1) | ||
| { | ||
| printf("\nSorry but something went wrong\n"); | ||
| return 1; | ||
| } | ||
|
|
||
| long long int iterativeAnswer = 0; | ||
| int errorCode1 = iterativeFibonacci(number, &iterativeAnswer); | ||
| if (errorCode1 != 0) | ||
| { | ||
| printf("\nSorry but something went wrong\n"); | ||
| return 1; | ||
| } | ||
|
|
||
| long long int recursiveAnswer = 0; | ||
| int errorCode2 = recursiveFibonacci(number, &recursiveAnswer); | ||
| if (errorCode2 != 0) | ||
| { | ||
| printf("\nSorry but something went wrong\n"); | ||
| return 1; | ||
| } | ||
|
|
||
| if (iterativeAnswer != recursiveAnswer) | ||
| { | ||
| printf("\nSorry but something went wrong\n"); | ||
| return 1; | ||
| } | ||
|
|
||
| printf("\nResult of work of iterative function: %lld", iterativeAnswer); | ||
| printf("\nResult of work of recursive function: %lld\n", recursiveAnswer); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| int iterativeFibonacci(int number) | ||
| int iterativeFibonacci(int number, long long int *answer) | ||
| { | ||
|
|
||
| if (number <= 3) | ||
| if (number < 1) | ||
| { | ||
| int firstThreeNumbers[3] = {1, 1, 2}; | ||
| return firstThreeNumbers[number - 1]; | ||
| *answer = -1; | ||
| return 1; | ||
| } | ||
|
|
||
| int previousNumber = 1; | ||
| int previousNumberForPreviousNumber = 1; | ||
| int currentNumber = 1; | ||
| int buffer = 0; | ||
| long long int currentNumbers[3] = {1, 1, 2}; | ||
|
|
||
| if (number < 4) | ||
| { | ||
| *answer = currentNumbers[number - 1]; | ||
| return 0; | ||
| } | ||
|
|
||
| for (int i = 2; i < number; i++) | ||
| for (int n = 2; n <= number; ++n) | ||
| { | ||
| previousNumberForPreviousNumber = previousNumber; | ||
| previousNumber = currentNumber; | ||
| currentNumber = previousNumber + previousNumberForPreviousNumber; | ||
| currentNumbers[2] = currentNumbers[1] + currentNumbers[0]; | ||
| currentNumbers[0] = currentNumbers[1]; | ||
| currentNumbers[1] = currentNumbers[2]; | ||
| } | ||
|
|
||
| return currentNumber; | ||
| *answer = currentNumbers[0]; | ||
| return 0; | ||
| } | ||
|
|
||
| int recursiveFibonacci(int number) | ||
| int recursiveFibonacci(int number, long long int *answer) | ||
| { | ||
| if (!correctInputCheckForRecursiveFunction(number)) | ||
| { | ||
| return 1; | ||
| } | ||
|
|
||
| *answer = recursiveFibonacciBody(number); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| long long int recursiveFibonacciBody(int number) | ||
| { | ||
| if (number == 0) | ||
| { | ||
| return 0; | ||
| } | ||
|
|
||
| if (number == 1) | ||
| { | ||
| return 1; | ||
| } | ||
| return recursiveFibonacciBody(number - 1) + recursiveFibonacciBody(number - 2); | ||
| } | ||
|
|
||
| return recursiveFibonacci(number - 2) + recursiveFibonacci(number - 1); | ||
| bool correctInputCheckForRecursiveFunction(int number) | ||
| { | ||
| if (number < 1) | ||
| { | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| int timeComparison() | ||
| bool testIterativeFunction(void) | ||
| { | ||
| float recursiveFunctionTimeOfWork = 1; | ||
| float iterativeFunctionTimeOfWork = 1; | ||
| int i = 1; | ||
| while (recursiveFunctionTimeOfWork / iterativeFunctionTimeOfWork < 2.0) | ||
| { | ||
|
|
||
| float startTimeOfWorkIterative, finishTimeOfWorkIterative; | ||
| startTimeOfWorkIterative = clock(); | ||
| int iterativeFunctionWorkResult = iterativeFibonacci(i); | ||
| finishTimeOfWorkIterative = clock(); | ||
| iterativeFunctionTimeOfWork = finishTimeOfWorkIterative - startTimeOfWorkIterative; | ||
|
|
||
| float startTimeOfWorkRecursive, finishTimeOfWorkRecursive; | ||
| startTimeOfWorkRecursive = clock(); | ||
| int recursiveFunctionWorkResult = recursiveFibonacci(i); | ||
| finishTimeOfWorkRecursive = clock(); | ||
| recursiveFunctionTimeOfWork = finishTimeOfWorkRecursive - startTimeOfWorkRecursive; | ||
|
|
||
| if (iterativeFunctionWorkResult != recursiveFunctionWorkResult) | ||
| { | ||
| printf("Different answers for same arguments"); | ||
| return 0; | ||
| } | ||
| printf("%f", recursiveFunctionTimeOfWork / iterativeFunctionTimeOfWork); | ||
|
|
||
| } | ||
| return i; | ||
| long long int test1 = 0; | ||
| int errorCode1 = iterativeFibonacci(-5, &test1); | ||
| if (errorCode1 != 1) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| long long int test2 = 0; | ||
| int errorCode2 = iterativeFibonacci(3, &test2); | ||
| if (test2 != 2 || errorCode2 != 0) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| long long int test3 = 0; | ||
| int errorCode3 = iterativeFibonacci(21, &test3); | ||
| if (test3 != 10946 || errorCode3 != 0) | ||
| { | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| int main() | ||
| bool testRecursiveFunction(void) | ||
| { | ||
| long long int test1 = 0; | ||
| int errorCode1 = recursiveFibonacci(-5, &test1); | ||
| if (errorCode1 != 1) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| printf("%d", timeComparison()); | ||
| long long int test2 = 0; | ||
| int errorCode2 = recursiveFibonacci(3, &test2); | ||
| if (test2 != 2 || errorCode2 != 0) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| return 0; | ||
| long long int test3 = 0; | ||
| int errorCode3 = recursiveFibonacci(21, &test3); | ||
| if (test3 != 10946 || errorCode3 != 0) | ||
| { | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| bool test(void) | ||
| { | ||
| if (!testIterativeFunction()) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| if (!testRecursiveFunction()) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } |
Oops, something went wrong.
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.
В PR не должно быть изменений в файлах, не связанных с задачей.