Every  positive integer number, that is not prime, may be decomposed in prime factors. For example the prime factors of 20, are:
```
2, 2, and 5, because: 20 = 2 . 2 . 5
```
The first prime factor (the smallest one) of ```20``` is ```2``` and the last one (the largest one) is ```5```. The sum of the first and the last prime factors, ```sflpf``` of 20 is: ```sflpf = 2 + 5 = 7```

The number ```998 ```is the only integer in the range ```[4, 1000]``` that has a value of ```501``` , so its ```sflpf``` equals to 501, but in the range ```[4, 5000]``` we will have more integers with ```sflpf = 501``` and are: ```998, 1996, 2994, 3992, 4990```.

We need a function ```sflpf_data()``` (javascript: ```sflpfData()```that receives two arguments, ```val``` as the value of sflpf and ```nMax``` as a limit, and the function will output a sorted list of the numbers between ```4``` to ```nMax```(included) that have the same value of sflpf equals to ```val```.

Let's see some cases:
```python
sflpf_data(10, 100) == [21, 25, 63]
/// the prime factorization of these numbers are:
Number  Prime Factorization     Sum First and Last Prime Factor
21       = 3 . 7      ---->                 3 + 7 = 10
25       = 5 . 5      ---->                 5 + 5 = 10
63       = 3 . 3 . 7  ---->                 3 + 7 = 10
```
```python
sflpf_data(10, 200) == [21, 25, 63, 105, 125, 147, 189]
sflpf_data(15, 150) == [26, 52, 78, 104, 130]
```
(Advice:Try to discard primes in a fast way to have a more agile code)

Enjoy it!

Source: codewars | Difficulty: EASY
Tags: Data structures

Mathematics

Starter code: def sflpf_data(val, nMax):
	

Solutions:
def sflpf_data(val, nMax):
	r = []
	for i in range(2, nMax):
		fac = primef(i)
		if len(fac) > 1 and fac[0] + fac.pop() == val:
			r.append(i)
	return r

def primef(n):
	i = 2
	f = []
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			f.append(i)
	if n > 1:
		f.append(n)
	return f


def sflpf_data(val, limit):
	result = []
	for n in range(4, limit + 1):
		factors = prime_factors(n)
		if len(factors) > 1 and min(factors) + max(factors) == val:
			result.append(n)
	return result

def prime_factors(n):
	factors = []
	while not n % 2:
		factors.append(2)
		n = n // 2
	while not n % 3:
		factors.append(3)
		n = n // 3
	k = 5
	step = 2
	while k <= n ** 0.5:
		if not n % k:
			factors.append(k)
			n = n // k
		else:
			k = k + step
			step = 6 - step
	factors.append(n)
	return factors


def sflpf_data(val, nMax):
	prime = [2, 3]
	result = []
	head = 1
	tail = 0
	for i in range(4, nMax):
		i_temp = i
		for j in prime:
			if i == 1 or j > val:
				break
			while i % j == 0:
				i /= j
				if head == 0:
					head = j
				tail = j
		if i == 1:
			if head + tail == val:
				result.append(i_temp)
		else:
			prime.append(i_temp)
		head = 0
	return result


import itertools

def sflpf(n):
	f = 2
	l = []
	increments = itertools.chain([1, 2, 2], itertools.cycle([4, 2, 4, 2, 4, 6, 2, 6]))
	for incr in increments:
		if f * f > n:
			break
		while n % f == 0:
			l.append(f)
			n //= f
		f += incr
	if n > 1:
		l.append(n)
	return min(l) + max(l) if len(l) > 1 else 0

def sflpf_data(val, nMax):
	return [i for i in range(4, nMax + 1) if sflpf(i) == val]


def sflpf(n):
	i = 2
	first = True
	small = 0
	large = 0
	while i * i <= n:
		while n % i == 0:
			large = i
			if first:
				small = i
				first = False
			n /= i
		i += 1
	if small:
		return small + max(large, n)
	else:
		return 0
sflpf_of = [sflpf(i) for i in range(1, 2 * 10 ** 4)]

def sflpf_data(val, n):
	return [x for x in range(1, n + 1) if sflpf_of[x - 1] == val]


def sflpf_data(a, b):
	lis = []
	for i in range(2, b + 1):
		temp = i
		con = 2
		if temp == 1:
			lis.append(1)
		else:
			lis1 = []
			while temp > 1:
				while temp % con == 0:
					lis1.append(con)
					temp //= con
				con += 1
				if con * con > temp:
					if temp > 1:
						lis1.append(temp)
					break
			con = lis1[0] + lis1[len(lis1) - 1]
			if con == a and len(lis1) != 1:
				lis.append(i)
	return lis