lec 1
and other things this is mad confusing
This commit is contained in:
commit
045d7367a8
11 files changed
+349
No files matched your search
@@ -0,0 +1,37 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
typedef struct
|
||||
{
|
||||
int real;
|
||||
int imag;
|
||||
} ldaComplex;
|
||||
|
||||
void print(ldaComplex* lda)
|
||||
{
|
||||
printf("%d + %di", lda->real, lda->imag);
|
||||
}
|
||||
// this is the best of my ability in c: returning string is a Fucking Mess
|
||||
// i would love to do this in rust
|
||||
|
||||
ldaComplex add(ldaComplex a, ldaComplex b)
|
||||
{
|
||||
return (ldaComplex){a.real + b.real, a.imag + b.imag};
|
||||
}
|
||||
ldaComplex multiply(ldaComplex a, ldaComplex b)
|
||||
{
|
||||
return (ldaComplex){a.real * b.real - a.imag * b.imag,
|
||||
a.real * b.imag + a.imag * b.real};
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
ldaComplex lda = {1, 2};
|
||||
ldaComplex lda2 = {5, 2};
|
||||
ldaComplex addRes = add(lda, lda2);
|
||||
ldaComplex multiRes = multiply(lda, lda2);
|
||||
|
||||
printf("("); print(&lda); printf(") + ("); print(&lda2); printf(") = "); print(&addRes);
|
||||
printf("\n("); print(&lda); printf(") * ("); print(&lda2); printf(") = "); print(&multiRes);
|
||||
return 0;
|
||||
}
|
||||
// ass
|
||||
@@ -0,0 +1,12 @@
|
||||
void findMax(int *max, int a) {
|
||||
if (a > *max) *max = a;
|
||||
};
|
||||
|
||||
#include <stdio.h>
|
||||
int main(){
|
||||
int c = 176;
|
||||
int f = 10;
|
||||
findMax(&f, c);
|
||||
printf("%d, %d", c, f);
|
||||
return 0;
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
# lab 1
|
||||
|
||||
## ex 1
|
||||
|
||||
Write a pseudocode and implement a program in C to swap the first and last digits
|
||||
of a positive integer.
|
||||
|
||||
## ex 2
|
||||
|
||||
Complete this given function `void findMax(int *max, int a)`, which assigns a value
|
||||
a to max if a > max.
|
||||
|
||||
## ex 3
|
||||
|
||||
Write a structure to represent complex numbers and complete operators: add and
|
||||
multiply.
|
||||
|
||||
## ex 4
|
||||
|
||||
Write a pseudo-code by commenting in the file then implement a program in to
|
||||
enter a natural number n and verify whether n is sphenic. Calculate the complexity
|
||||
of your program.
|
||||
Note: A sphenic number is a product of $p \times q \times r$ where $p$, $q$, and $r$ are three distinct
|
||||
prime numbers. Example: $30 = 2 \times 3 \times 5$; $42 = 2 \times 3 \times 7$; $66 = 2 \times 3 \times 11$
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int log10intint(int n)
|
||||
{
|
||||
// as int goes to 2^64 we gon
|
||||
if (n <= 0)
|
||||
return 1 << 31;
|
||||
int a = 1;
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
a *= 10;
|
||||
if (n - a < 0)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
};
|
||||
int ten_to_the(int i)
|
||||
{ if (i < 0) return 0;
|
||||
int ret = 1;
|
||||
while (i-- > 0)
|
||||
ret *= 10;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int special_swap(int number)
|
||||
{
|
||||
int first_number, second_number, third_number;
|
||||
int this_number = ten_to_the(log10intint(number));
|
||||
|
||||
third_number = number % 10;
|
||||
first_number = number / this_number;
|
||||
second_number = number % this_number - third_number;
|
||||
|
||||
return second_number + third_number * this_number + first_number;
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
printf("%d\n", special_swap(142425));
|
||||
return 0;
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
bool sphenic(int i)
|
||||
{
|
||||
if (i < 30)
|
||||
return false;
|
||||
|
||||
// number can be prime ^ a * prime ^ b * prime ^ c * other;
|
||||
// idea straight from stack overflowing
|
||||
|
||||
int prime_prod = 1;
|
||||
int count = 0; // handle case 3*11 for example
|
||||
int sphenic = 3;
|
||||
|
||||
for (
|
||||
int n = i, p = 2;
|
||||
count < sphenic // we have 3 numbers
|
||||
&& n != 1 // we have divided all the numbers
|
||||
&& p <= n; // lil check
|
||||
p++)
|
||||
|
||||
if (n % p == 0)
|
||||
{
|
||||
count++;
|
||||
prime_prod *= p;
|
||||
while (n % p == 0)
|
||||
n /= p; // what if you can be divided by 4?
|
||||
};
|
||||
|
||||
return prime_prod == i // see thats what the while is for
|
||||
&& count == sphenic; // what if we have 17 * 29
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int N = 200;
|
||||
|
||||
for (int i = 1; i < N; i++)
|
||||
if (sphenic(i))
|
||||
printf("%d ", i);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user